from os import strerror
srcname = input("Enter the source file name: ")
try:
src = open(srcname, 'rb')
except IOError as e:
print("Cannot open the source file: ", strerror(e.errno))
exit(e.errno)
dstname = input("Enter the destination file name: ")
try:
dst = open(dstname, 'wb')
except Exception as e:
print("Cannot create the destination file: ", strerror(e.errno))
src.close()
exit(e.errno)
buffer = bytearray(65536)
total = 0
try:
readin = src.readinto(buffer)
print(readin)
while readin > 0:
written = dst.write(buffer[:readin])
print(written)
total += written
readin = src.readinto(buffer)
print(readin)
except IOError as e:
print("Cannot create the destination file: ", strerror(e.errno))
exit(e.errno)
print(total,'byte(s) succesfully written')
src.close()
dst.close()
#written = dst.write(buffer[:readin])如何认定这就是读取的文件
因为你是通过src.readin(buffer)读取的,将内容读取到buffer中。
然后写入dst中,操作的是butter。
#读取src文件中的内容,将内容存储buffer变量中
readin = src.readinto(buffer)
#输出当前读取内容的有效长度
print(readin)
#如果当前有效长度大于0,说明文件中存在内容,循环遍历读取
while readin > 0:
#边读边写
#将buffer中的内容写入dst文件对象中
written = dst.write(buffer[:readin])
#打印当前写入的内容长度
print(written)
#统计所有写入文件中的内容长度
total += written
#继续读取
readin = src.readinto(buffer)
print(readin)
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!