Matt
Matt

Reputation: 1119

Python FTP invalid argument

Python 3.6.1, Windows 10 x64

My goal with this script is to grab a .zip file from a FTP site and then put it into a local directory:

from ftplib import FTP
FTP_SERVER = 'ftp.name.com'

def main():
    ftp_conn = FTP(FTP_SERVER)

    ftp_conn.login('username','password')

    get_binary_file(ftp_conn, 'file.zip', 'C:\Temp\test.zip')

    ftp_conn.close()

def get_binary_file(ftp_client, file_name, dest=None):
    if not dest:
        dest = file_name

    ftp_cmd = 'RETR {}'.format(file_name)

    with open(dest,'wb') as dest_in:
        ftp_client.retrbinary(
            ftp_cmd,
            dest_in.write

        )


if __name__ == '__main__':
    main()

I keep getting an error that the 3rd argument (C:\Temp\test.zip) is invalid:

Traceback (most recent call last):
  File "C:/code/ftp_dl_binary_file1.py", line 32, in <module>
    main()
  File "C:/code/ftp_dl_binary_file1.py", line 13, in main
    get_binary_file(ftp_conn, 'file.zip', 'C:\Temp\test.zip')
  File "C:/code/ftp_dl_binary_file1.py", line 23, in get_binary_file
    with open(dest,'wb') as dest_in:
OSError: [Errno 22] Invalid argument: 'C:\\Temp\test.zip'

What is invalid about the argument?

Upvotes: 2

Views: 698

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169314

The \t in \test.zip is being interpreted as a tab control character.

You should instead do:

    get_binary_file(ftp_conn, 'file.zip', 'C:/Temp/test.zip')

Or:

    get_binary_file(ftp_conn, 'file.zip', 'C:\\Temp\\test.zip')

Upvotes: 3

Related Questions