我有一个类似于下面的场景。我的目标是打印出从Python retrlines("LIST")函数中检索的文件行。任何帮助都是非常感谢的。
class FTP:
def __init__(self, hostName, userName, passWord, encoding=None):
self.ftp = ftplib.FTP(host=hostName, user=userName, passwd=passWord)
self.defaultEncoding = self.ftp.encoding
self.ftp.encoding = encoding or self.ftp.encoding
def writeTest(self, fileName, func):
self.ftp.retrlines(f'RETR {fileName}', func)
def getFileInformation(self, func=None):
encoding = self.ftp.encoding
self.ftp.encoding = self.defaultEncoding
self.ftp.retrlines('LIST', lambda row: func(self._returnFileInformation(row)))
self.ftp.encoding = encoding
class FileProcessor:
def __init__(self, processingFunction: Callable, output: Callable):
self.processingFunction = processingFunction
self.output = output
self.fileCounter = 0
def processFile(self, file: FileInformation):
if self.processingFunction(file):
self.output(file)
self.fileCounter += 1
def __len__(self):
return self.fileCounter
lastReadTime = datetime(2020, 10, 29, 0, 0, 0)
ftp = FTP(hostName=hostName, userName=userName, passWord=passWord, encoding='utf-16')
processor = FileProcessor(lambda file: file.timeStamp > lastReadTime, lambda file: ftp.writeTest(file.name, lambda line: print(line)))
ftp.getFileInformation(processor.processFile)getFileInformation只是在一个格式良好的类中返回来自retrlines LIST函数的值,该类具有名称、大小等。我只是试图打印文件的行,因为我从retrlines('LIST')获取文件名。如果我先检索文件名,然后在之后处理这些文件,则没有问题。如果我一次试着做完这件事,我就会得到一个类似于这样的错误:

发布于 2021-02-18 06:37:16
您不能下载文件,而您仍然使用相同的连接下载目录列表。无论您使用的是什么FTP库,FTP协议都不可能这样做。关于ftplib :不能调用FTP类(不能调用FTP.retrlines('RETR ...')),而另一个方法(FTP.retrlines('LIST ...'))仍在执行。
以下任一项:
https://stackoverflow.com/questions/66252547
复制相似问题