ChatDev/WareHouse/Good_code_ModelBest1024_20231023013519/file_processor.py
2023-10-23 11:09:56 +08:00

26 lines
1.0 KiB
Python

'''
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}")