Python在上传大小大于8192字节的文件时失败。唯一的例外是“有超过8192字节”。是否有上传更大文件的解决方案。
try:
ftp = ftplib.FTP(str_ftp_server )
ftp.login(str_ftp_user, str_ftp_pass)
except Exception as e:
print('Connecting ftp server failed')
return False
try:
print('Uploading file ' + str_param_filename)
file_for_ftp_upload = open(str_param_filename, 'r')
ftp.storlines('STOR ' + str_param_filename, file_for_ftp_upload)
ftp.close()
file_for_ftp_upload.close()
print('File upload is successful.')
except Exception as e:
print('File upload failed !!!exception is here!!!')
print(e.args)
return False
return True发布于 2015-01-19 00:52:21
storlines一次只读取一行文本文件,8192是每一行的最大大小。您最好使用,作为上传功能的核心:
with open(str_param_filename, 'rb') as ftpup:
ftp.storbinary('STOR ' + str_param_filename, ftpup)
ftp.close()它以二进制方式读取和存储,每次一个块(相同的默认值为8192),但是对于任何大小的文件都应该很好。
发布于 2017-06-13 19:32:56
我有一个类似的问题,并通过增加ftplib的maxline变量的值来解决它。您可以将其设置为任意整数值。它表示文件中每行的最大字符数。这会影响上传和下载。
在大多数情况下,根据Alex的回答,我建议使用ftp.storbinary,但在我的情况下,这不是一个选项(不是标准)。
ftplib.FTP.maxline = 16384 # This is double the default value在开始文件传输之前,只要在任何时候调用该行即可。
https://stackoverflow.com/questions/28016444
复制相似问题