我编写了以下代码,目的是将 abc.txt 的内容复制到另一个文件 xyz.txt
但该声明b_file.write(a_file.read())
似乎没有按预期工作。如果我用某个字符串替换 a_file.read() ,它(字符串)就会被打印出来。
import locale
with open('/home/chirag/abc.txt','r', encoding = locale.getpreferredencoding()) as a_file:
print(a_file.read())
print(a_file.closed)
with open('/home/chirag/xyz.txt','w', encoding = locale.getpreferredencoding()) as b_file:
b_file.write(a_file.read())
with open('/home/chirag/xyz.txt','r', encoding = locale.getpreferredencoding()) as b_file:
print(b_file.read())
我该怎么做?