-2

I have a zip file to upload. I know how to upload it.I open the file with "Rb" mode. When i want to extract the zip file that i uploaded i get an error and files in the ZIP archive are gone, i think that's because of the "Rb" mode . I don't know how to extract my uploaded file.

Here is the code:

filename="test.zip"
ftp=ftplib.FTP("ftp.test.com")
ftp.login('xxxx','xxxxx')
ftp.cwd("public_html/xxx")
myfile=open("filepath","rb")
ftp.storlines('STOR ' + filename,myfile)
ftp.quit()
ftp.close()
MR.crazy
  • 3
  • 2
  • 1
    hi, we'd love to help, but it would be a big step if you can paste the problematic code here, this will help you understand what code you should add : https://stackoverflow.com/help/mcve – Avishay Cohen Dec 19 '17 at 16:08
  • 1
    One assumes you need to tell the FTP client/server to use binary mode as well? I think you mean `.storbinary()` maybe? This is discussed in the ftlib API docs. –  Dec 19 '17 at 16:26

1 Answers1

0

Your code is currently using ftp.storlines() which is intended for use with ASCII files.

For binary files such as ZIP files, you need to use ftp.storbinary() instead:

import ftplib

filename = "test.zip"    

with open(filename, 'rb') as f_upload:
    ftp = ftplib.FTP("ftp.test.com")
    ftp.login('xxxx', 'xxxxx')
    ftp.cwd("public_html/xxx")
    ftp.storbinary('STOR ' + filename, f_upload)
    ftp.quit()
    ftp.close()    

When ASCII mode is used on a ZIP file, it will result in an unusable file which is what you were getting.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97