文件拷贝
# 复制文件小脚本 source_file = r'D:\py1912\demo\tanchishe.py' target_file = r'D:\py1912\demo\temp.py' # 分别打开文件对象 source = open(source_file,'rb') target = open(target_file,'wb') # 从源文件中读取指定数量字节 1024B buffer_size = 10 # 缓冲区大小 data = source.read(buffer_size) # 只要数据对象不是空字节对象,我们就认为还有数据 while data != b"": # 向目标文件中写入数据 target.write(data) # 继续读取数据 data = source.read(buffer_size) # 关闭文件对象 source.close() target.close()