我正在尝试将现有文件的每一行写入两个新文件。基本上这将复制文件。“oldMaster.write(line)”返回一个错误,指出该文件不可写。我知道我的代码很糟糕。这是一个项目,我真的被卡住了。
file_str = input ("Enter a file name: ")
while True:
try:
file_obj = open(file_str, 'r')
break
except IOError:
file_str = input("'Invalid file name' Enter a file name: ")
prefix = input("Master File Prefix?")
old = prefix
old += ".old.txt"
new = prefix
new += ".new.txt"
oldMaster = open(old,"w")
newMaster = open(new,"w")
oldMaster = file_obj
newMaster = file_obj
for line_str in file_obj:
line = line_str
oldMaster.write(line)