bah*_*tan 9 python ftp file-upload
上传文件大小超过8192字节时,Python失败.而例外只是"超过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
Run Code Online (Sandbox Code Playgroud)
storlines一次读取一行文本文件,8192是每行的最大大小.作为上传功能的核心,您可能最好使用它:
with open(str_param_filename, 'rb') as ftpup:
ftp.storbinary('STOR ' + str_param_filename, ftpup)
ftp.close()
Run Code Online (Sandbox Code Playgroud)
它以二进制形式读取和存储,一次一个块(默认值为8192),但对于任何大小的文件都应该可以正常工作.