bahtiyartan
bahtiyartan

Reputation: 1030

FTP library error: got more than 8192 bytes

Python fails while uploading a file which its size bigger than 8192 bytes. And the exception is only "got more than 8192 bytes". Is there a solution to upload larger files.

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

Upvotes: 10

Views: 5717

Answers (2)

Hans Goldman
Hans Goldman

Reputation: 574

I had a similar issue and solved it by increasing the value of ftplib's maxline variable. You can set it to any integer value you wish. It represents the maximum number of characters per line in your file. This affects uploading and downloading.

I would recommend using ftp.storbinary in most cases per Alex Martelli's answer, but that was not an option in my case (not the norm).

ftplib.FTP.maxline = 16384    # This is double the default value

Just call that line at any point before you start the file transfer.

Upvotes: 4

Alex Martelli
Alex Martelli

Reputation: 881675

storlines reads a text file one line at a time, and 8192 is the maximum size of each line. You're probably better off using, as the heart of your upload function:

with open(str_param_filename, 'rb') as ftpup:
    ftp.storbinary('STOR ' + str_param_filename, ftpup)
    ftp.close()

This reads and stores in binary, one block at a time (same default of 8192), but should work fine for files of any size.

Upvotes: 12

Related Questions