-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy path1.python_wrap_lines.py
More file actions
42 lines (34 loc) · 1.16 KB
/
1.python_wrap_lines.py
File metadata and controls
42 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
size = 80
file = 'budo'
folder = os.path.expanduser('~/Documents/Fortunes/')
# Read and store the entire file line by line
with open(f'{folder}{file}.txt') as reader:
provers = reader.readlines()
# wrap/collate lines by separators [",", " ", "."]
def collate(text, size):
new_text = []
split_char = 1
while split_char > 0:
comma = str.find(text, ',', size)
space = str.find(text, ' ', size)
dot = str.find(text, '.', size)
split_char = min(max(comma, dot), max(comma, space), max(dot, space))
if text[:split_char]:
new_text.append(text[:split_char])
text = text[split_char+1:].replace('\n', "")
return new_text
# write collated information to new(same) file
with open(f'{folder}{file}.txt', 'w') as writer:
for wisdom in provers:
if len(wisdom) > size:
collated = collate(wisdom, size)
for short in collated:
writer.write(short)
writer.write('\n')
else:
writer.write(wisdom)
# Executing Shell Commands with Python
import os
myCmd = f'strfile -c % {folder}{file}.txt {folder}{file}.txt.dat'
os.system(myCmd)