0
from threading import Thread
src_file = 'test.txt'
dst_file = 'test_copy.txt'
def cb(file_size):
print("Copied: {}\n".format(file_size))
def copyfileobj(fsrc, fdst, callback, length=8*1024):
copied = 0
while True:
buff = fsrc.read(length)
if not buff:
break
fdst.write(buff)
copied += len(buff)
callback(copied)
t = Thread(target=copyfileobj, args=[src_file, dst_file, cb]).start()
進步當我運行此我得到:試圖從copyfileobj獲得在Python
buf = fsrc.read(length) AttributeError: 'str' object has no attribute 'read'
我應該如何作出這樣的fsrc
已經閱讀屬性?
您需要'打開(fsrc)作爲f'來獲取句柄.'read'在文件句柄上工作,而不是文件名。 –