使用Python从FTP下载带有内容的ZIP

时间:2018-10-12 21:23:20

标签: python ftp ftplib

我正在尝试从FTP下载一堆zip文件。我的代码运行了,但是zip文件是空的(而不是包含原始内容)。

您能帮我下载文件夹内容吗?注意,我不想递归地打开和下载内容。

我是新的StackExchange,对Python来说还比较陌生,所以请谅解。

我当前的代码:

from ftplib import FTP
import ftplib
import os
import sys

ftp = FTP('somelink.com')
ftp.login()
scratch_path = r"C:\somepath"
f_list = ftp.nlst()

#iterates through zip files and downloads files that have not yet been downloaded 
def download_fundamentals(file_list,local_path):

    for zip_file in file_list:
        print(zip_file)
        os.chdir(local_path)
        f = open(zip_file, 'wb')
        #this was the source of error!
        if Path(scratch_path+'/'+zip_file).exists():
            continue
        #attempts to download zip file
        try:
            ftp.retrbinary('RETR ' + zip_file,f.write)
            print('Download successful for '+zip_file)
        except:
            print('Error downloading '+zip_file)

    print('Finished downloading files')

    ftp.close()

download_fundamentals(f_list,scratch_path)

编辑:我整理了一些不同领域的代码-弄乱了命令,对不起!还删除了登录信息。我已经对其进行了编辑,因此代码更具意义。我的原始问题-为什么内容无法下载/如何解决?仍然存在!

编辑2:下面的日志文件:

*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type set to A.\n'
*resp* '200 Type set to A.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (159,220,68,250,42,41)\n'
*resp* '227 Entering Passive Mode (159,220,68,250,42,41)'
*cmd* 'NLST'
*put* 'NLST\r\n'
*get* '150 Opening ASCII mode data connection for file list.\n'
*resp* '150 Opening ASCII mode data connection for file list.'
*get* '226 Transfer complete. 6936 bytes transferred. 6936 bps.\n'
*resp* '226 Transfer complete. 6936 bytes transferred. 6936 bps.'

file_1.zip
file_2.zip
....
file_m.zip
Finished downloading files

0 个答案:

没有答案