Tag: ftplib

如何使用Python ftplib通过FTP下载文件

我有以下代码可以轻松连接到FTP服务器并打开一个zip文件。 我想将该文件下载到本地系统中。 怎么做? # Open the file for writing in binary mode print 'Opening local file ' + filename file = open(filename, 'wb') # Download the file a chunk at a time # Each chunk is sent to handleDownload # We append the chunk to the file and then print a '.' for progress # […]

检查FTP服务器上的对象是使用Python和ftplib的文件或目录

使用Python和ftplib,我正在编写一个通用函数来检查FTP目录中的项目是文件还是目录。 由于使用MLSD函数可能不一定适用于所有的服务器(我的一个用例不提供),我已经采取了这种有效但粗糙的方式来确定它,试图改变目录到对象,如果对象是一个文件,引发一个exception,并相应地设置文件types。 file_type = '' try: ftp.cwd(item_name) file_type = 'dir' ftp.cwd(cur_path) except ftplib.error_perm: file_type = 'file' 我已经search了其他方法的互联网和图书馆文件,但我找不到在大多数情况下工作的。 例如,使用dir函数,我可以检查第一个字符是否是'd' ,这可能会确定它,但是进一步阅读已经指出并不是所有的输出格式都是相同的。 我在这个方法中看到的最大的缺陷是如果我没有权限将目录改变到指定的文件夹; 因此将被视为一个文件。 有什么我失踪或更干净的方式来做到这一点?

使用ftplib下载目录树

这不会下载子目录的内容; 我该怎么办? import ftplib import configparser import os directories = [] def add_directory(line): if line.startswith('d'): bits = line.split() dirname = bits[8] directories.append(dirname) def makeDir(archiveTo): for dir in directories: newDir = os.path.join(archiveTo, dir) if os.path.isdir(newDir) == True: print("Directory \"" + dir + "\" already exists!") else: os.mkdir(newDir) def getFiles(archiveTo, ftp): files = ftp.nlst() for filename in […]