Python FTP无效参数

时间:2017-04-14 20:51:03

标签: python-3.x ftp ftplib

Python 3.6.1,Windows 10 x64

我使用此脚本的目标是从FTP站点获取.zip文件,然后将其放入本地目录:

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()

我不断收到第3个参数(C:\ Temp \ test.zip)无效的错误:

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'

这个论点有什么不妥?

1 个答案:

答案 0 :(得分:3)

\t中的\test.zip被解释为标签control character

你应该这样做:

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

或者:

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