下面给出一个文本文件复制示例。
代码如下:
f_name = r'D:\mytest2.txt'
with open(f_name, 'r', encoding='gbk') as f:
lines = f.readlines()
copyfile = r'D:\copytest2.txt'
with open(copyfile, 'w', encoding='utf-8') as copy_f:
copy_f.writelines(lines)
print('文件复制成功')
结果:
文件复制成功
进程已结束,退出代码为 0
pycharm运行截图如下:
复制文件结果如下: