Skip to main content
 首页 » 编程设计

python之尝试在 2 个文件之间切换文本

2024年10月25日8youxin

from sys import argv 
 
script, f1, f2 = argv 
 
print """This program is going to switch the  
content of the two files you just selected.""" 
 
first_file = open(f1, 'r+') 
first_content = first_file.read() 
 
 
second_file = open(f2, 'r+') 
second_content = second_file.read() 
 
 
final_first = first_file.write(second_content) 
final_second = second_file.write(first_content) 

IOError: [Errno 0] 错误


我正在尝试同时在两个文本文件之间切换内容。想法?

请您参考如下方法:

您已到达 EOF(文件末尾)。

在写入之前尝试调用 first_file.seek(0,0) 和 second_file.seek(0, 0)

有更简单的方法来交换文件内容,例如通过重命名文件