gpt4 book ai didi

Python shutil copyfile - 缺少最后几行

转载 作者:太空狗 更新时间:2023-10-29 22:29:55 24 4
gpt4 key购买 nike

我经常丢失我尝试使用 shutil copyfile 复制的文件的最后几 kb。

我做了一些研究,确实看到有人在这里问类似的问题: python shutil copy function missing last few lines

但我正在使用 copyfile,它似乎确实使用了 with 语句...

with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
copyfileobj(fsrc, fdst)

所以我很困惑,更多的用户没有遇到这个问题,如果确实是某种缓冲问题 - 我认为它会更广为人知。

我很简单地调用了 copyfile,不要认为我可能做错了什么,本质上是按照我认为的标准方式做的:

copyfile(target_file_name,dest_file_name) 

但我每次都丢失文件的最后 4kb 左右。

我也没有触及在 shutil 中调用的复制文件函数,它是......

def copyfileobj(fsrc, fdst, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)

所以我不知所措,但我想我将要学习一些关于刷新、缓冲或 with 语句的知识,或者...帮助!谢谢


致阿南德:阿南德,我避免提及这些东西,因为我觉得这不是问题所在,但自从你问过...执行摘要是我从 FTP 抓取文件,检查文件是否与我上次保存的文件不同复制,如果是,下载文件并保存副本。这是迂回的意大利面条代码,我猜是在我还是一个真正纯粹的功利主义编码新手时编写的。看起来像:

for filename in ftp.nlst(filematch):
target_file_name = os.path.basename(filename)
with open(target_file_name ,'wb') as fhandle:
try:
ftp.retrbinary('RETR %s' % filename, fhandle.write)
the_files.append(target_file_name)
mtime = modification_date(target_file_name)
mtime_str_for_file = str(mtime)[0:10] + str(mtime)[11:13] + str(mtime)[14:16] + str(mtime)[17:19] + str(mtime)[20:28]#2014-12-11 15:08:00.338415.
sorted_xml_files = [file for file in glob.glob(os.path.join('\\\\Storage\\shared\\', '*.xml'))]
sorted_xml_files.sort(key=os.path.getmtime)
last_file = sorted_xml_files[-1]
file_is_the_same = filecmp.cmp(target_file_name, last_file)
if not file_is_the_same:
print 'File changed!'
copyfile(target_file_name, '\\\\Storage\\shared\\'+'datebreaks'+mtime_str_for_file+'.xml')
else:
print 'File '+ last_file +' hasn\'t changed, doin nothin'
continue

最佳答案

这里的问题很可能是,在执行行时 -

ftp.retrbinary('RETR %s' % filename, fhandle.write)

这是使用 fhandle.write() 函数将数据从 ftp 服务器写入文件(名称为 - target_file_name),但是当您正在调用 -shutil.copyfile - fhandle 的缓冲区尚未完全刷新,因此您在复制文件时丢失了一些数据。

为确保不会发生这种情况,您可以将 copyfile 逻辑移出 fhandlewith block 。

或者您可以在复制文件之前调用 fhandle.flush() 刷新缓冲区。

我相信关闭文件会更好(将逻辑移出 with block )。示例 -

for filename in ftp.nlst(filematch):
target_file_name = os.path.basename(filename)
with open(target_file_name ,'wb') as fhandle:
ftp.retrbinary('RETR %s' % filename, fhandle.write)
the_files.append(target_file_name)
mtime = modification_date(target_file_name)
mtime_str_for_file = str(mtime)[0:10] + str(mtime)[11:13] + str(mtime)[14:16] + str(mtime)[17:19] + str(mtime)[20:28]#2014-12-11 15:08:00.338415.
sorted_xml_files = [file for file in glob.glob(os.path.join('\\\\Storage\\shared\\', '*.xml'))]
sorted_xml_files.sort(key=os.path.getmtime)
last_file = sorted_xml_files[-1]
file_is_the_same = filecmp.cmp(target_file_name, last_file)
if not file_is_the_same:
print 'File changed!'
copyfile(target_file_name, '\\\\Storage\\shared\\'+'datebreaks'+mtime_str_for_file+'.xml')
else:
print 'File '+ last_file +' hasn\'t changed, doin nothin'
continue

关于Python shutil copyfile - 缺少最后几行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31546902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com