Python ftplib损坏文件? [英] Python ftplib Corrupting Files?
问题描述
我正在使用ftplib在Python中下载文件,直到最近一切似乎都工作正常.我正在这样下载文件:
I'm downloading files in Python using ftplib and up until recently everything seemed to be working fine. I am downloading files as such:
ftpSession = ftplib.FTP(host,username,password)
ftpSession.cwd('rlmfiles')
ftpFileList = filter(lambda x: 'PEDI' in x, ftpSession.nlst())
ftpFileList.sort()
for f in ftpFileList:
tempFile = open(os.path.join(localDirectory,f),'wb')
ftpSession.retrbinary('RETR '+f,tempFile.write)
tempFile.close()
ftpSession.quit()
sys.exit(0)
直到最近,它仍可以按预期下载我需要的文件.但是,现在,我正在下载的我的文件"已损坏,仅包含一长串垃圾ASCII.我知道不是将文件发布到FTP上,因为我也有一个Perl脚本可以从同一FTP成功完成此操作.
Up until recently it was downloading the files I needed just fine, as expected. Now, however, My files I'm downloading are corrupted and just contain long strings of garbage ASCII. I know that it is not the files posted onto the FTP I'm pulling them from because I also have a Perl script that does this successfully from the same FTP.
如果有任何其他信息,这是调试器在下载文件时在命令提示符中显示的内容:
If it is any additional info, here's what the debugger puts out in the command prompt when downloading a file:
有人使用Python的ftplib中的 retrbinary()
遇到文件损坏的问题吗?
Has anyone encountered any issues with corrupted file contents using retrbinary()
in Python's ftplib?
我真的很困/沮丧,并且在这里没有遇到任何与可能的腐败有关的事情.任何帮助表示赞赏.
I'm really stuck/frustrated and haven't come across anything related to possible corruption here. Any help is appreciated.
推荐答案
昨天我在尝试下载文本文件时遇到了这个问题.不知道那是不是在做什么,但是由于您说它里面有ASCII垃圾,我假设您在文本编辑器中打开了它,因为它应该是文本.
I just ran into this issue yesterday when I was attempting to download text files. Not sure if that is what you were doing, but since you say it has ASCII garbage in it, I assume you opened it in a text editor because it was supposed to be text.
在这种情况下,问题在于该文件是文本文件,您正尝试以二进制模式下载它.
If this is the case, the problem is that the file is a text file and you are trying to download it in binary mode.
您要做的是以ASCII传输模式检索文件.
What you want to do instead is retrieve the file in ASCII transfer mode.
tempFile = open(os.path.join(localDirectory,f),'w') # Changed 'wb' to 'w'
ftpSession.retrlines('RETR '+f,tempFile.write) # Changed retrbinary to retrlines
不幸的是,这会将所有换行符从文件中删除.uck!
Unfortunately, this strips all the new-line characters out of the file. Yuck!
因此,您需要再次添加删除的换行符:
So then you need to add the stripped out new-line characters again:
tempFile = open(os.path.join(localDirectory,f),'w')
textLines = []
ftpSession.retrlines('RETR '+f,textLines.append)
tempFile.write('\n'.join(textLines))
这应该可以,但是看起来不尽如人意.因此,只需进行一些清理工作即可:
This should work, but it doesn't look as nice as it could. So a little cleanup effort would get us:
temporaryFile = open(os.path.join(localDirectory, currentFile), 'w')
textLines = []
retrieveCommand = 'RETR '
ftpSession.retrlines(retrieveCommand + currentFile, textLines.append)
temporaryFile.write('\n'.join(textLines))
这篇关于Python ftplib损坏文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!