【发布时间】:2020-04-05 22:11:09
【问题描述】:
我需要编辑刚刚使用copyFile() 创建的文件。
但是,如果在'w' 模式或'w+' 或'a' 模式下打开文件,readlines() 总是会失败。
copyfile(fileObj.file_to_open, fileObj._copyToFileName)
with open(fileObj._copyToFileName, 'w') as thisFile:
lines = thisFile.readlines()
如果我将其保留为默认模式(读取模式),我可以readlines()。
为什么使用写入模式打开会成为问题?
【问题讨论】:
-
你以
write mode打开文件,你需要以read mode打开它,这意味着:with open(fileObj._copyToFileName, 'r') as thisFile: