forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_processor.py
26 lines (26 loc) · 1.04 KB
/
file_processor.py
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
'''
This file contains a function to process the selected Python file.
The function reads the file, adds praises to each line, and writes the result to a new file.
The new file has the same name as the original file, but with "praised_" added at the beginning.
'''
from praise_generator import generate_praise
def process_file(filename):
try:
with open(filename, "r", encoding="utf8") as file:
lines = file.readlines()
except IOError as e:
print(f"Unable to open file: {e}")
return
new_lines = []
for line in lines:
if line.strip() and not line.strip().startswith("#"):
# Check if there is already a comment on the line
if "#" not in line:
line = line.rstrip() + " # " + generate_praise(line) + "\n"
new_lines.append(line)
try:
with open("praised_" + filename.split('/')[-1], "w", encoding="utf8") as file:
for line in new_lines:
file.write(line)
except IOError as e:
print(f"Unable to write to file: {e}")