import os
def file_copy(src, des):
# 打开源文件
file_r = open(src, 'rb') # 这里使用rb能够拷贝二进制文件
# 打开要拷贝进的文件
file_w = open(des, 'wb')
while True:
# 读取内容
content = file_r.read(1024)
# 判断是否读取完
if content == b'':
print('over')
break
file_w.write(content)
file_w.close()
file_r.close()
file_copy('Z:/Codes/Python/te.py', 'Z:/Codes/Python/test.py')
遇到过的错误:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb6 in position 61: illegal multibyte sequence
这里就是需要加上 b 来进行二进制读取
ValueError: read of closed file
提前关闭了文件。