Python FTP无法发送完整文件 [英] Python FTP not sending full files

查看:74
本文介绍了Python FTP无法发送完整文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这段代码,该代码旨在通过FTP发送文件.问题在于,在3/5的情况下,文件没有完全移到另一端-我知道您必须使用检索.我正在编写此代码以供工作,因此请忽略代码中令人讨厌的已编辑"部分.当前,唯一可以完全到达另一端的文件是.bin文件和Image文件.-它们是两个最小的文件,其他两个文件都很大(最大文件为19000 kb.)下载文件时,它们与原始大小相比非常小.35 kb,而10000 kb.141 kb,而14000 kb.等等

I have written this code that's suppose to send files over FTP. The problem is that in 3/5 cases the files are not fully getting to the other side -- I understand you have to use retrieve. I am writing this code for work so ignore the obnoxious "REDACTED" parts of the code. Currently, the only files that are reaching the other side in full are the .bin file and the Image file. -- They are the two smallest files as the other ones are quite big (the biggest is 19000 kb.) When the files are downloaded, they are very small compared to their original size. 35 kb compared to 10000 kb. 141 kb compared to 14000 kb. etc.

ftp = FTP('REDACTED')  
ftp.login('REDACTED', 'REDACTED')
print ("Opening FTP...\n")
   with open ('REDACTED.gz.uboot') as f: 
    print ('writing file...REDACTED.gz.uboot') 
    ftp.storbinary('STOR %s' % 'REDACTED.gz.uboot', f)

with open('REDACTED_Image', 'rb') as x:
    print ("writing file...REDACTED_Image")
    ftp.storbinary('STOR %s' % 'Image', f)

with open('REDACTED.rbf', 'rb') as f:  
    ftp.storbinary('STOR %s' % 'huh.rbf', f)
    print ("writing file...m0_host_top.rbf")

with open ('REDACTED.tar.bz2') as f:
    print ('writing file...REDACTED')
    ftp.storbinary('STOR %s' % 'REDACTED', f)

with open ('REDACTED.bin') as f:
    print ('writing file...REDACTED.bin')
    ftp.storbinary('STOR %s' % 'REDACTED.bin', f)

这里的任何帮助将不胜感激,我需要获得完整的文件.我假设上载有问题,因为每次上载文件时,它们的大小都不同.

Any help here would be greatly appreciated, I need to get the full files across. I am presuming that there is an issue with the upload because each time the files are uploaded, they are of different sizes.

推荐答案

以二进制模式发送所有文件时,应以"rb"模式打开所有文件.

As you are sending all the file in binary mode, you should open all the files with 'rb' mode.

默认 open()将使用文本模式打开文件,这意味着将所有内容视为文本.但是,您有一些二进制文件,例如 REDACTED.tar.bz2 在上述情况下.使用文本模式读取这些文件将使文件内容与原始文件不同.

Default open() will use the text mode to open a file, which means treats all the content as text.However you have some binary file like REDACTED.tar.bz2 in the case above. Using text mode to read these files will make the file content different from the origin one.

此问题可能会在 open()上为您提供帮助:

This question may help you on open() : Difference between parsing a text file in r and rb mode:

这篇关于Python FTP无法发送完整文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆