我正在尝试使用 for 循环从 ftp 站点下载多个文件。在 python.exe 关闭窗口弹出之前,以下代码似乎仅适用于循环中的前 2 个文件。两个下载的文件是完美的,但是第三个下载的文件在关机时是空的。我没有得到其余的文件。知道可能是什么问题吗?
from PyQt4 import QtCore, QtGui, QtNetwork
class FtpWindow(QtGui.QDialog):
def __init__(self, parent=None):
self.fileList = QtGui.QTreeWidget()
self.ftp = QtNetwork.QFtp(self)
self.progressDialog = QtGui.QProgressDialog(self)
self.downloadAllButton.clicked.connect(self.downloadAllFile)
self.ftp.commandFinished.connect(self.ftpCommandFinished)
def downloadAllFile(self):
for jj in range(self.fileList.topLevelItemCount()): # how many files in a particular folder
fileName = self.fileList.topLevelItem(jj).text(0)
self.outFile = QtCore.QFile(fileName)
self.ftp.get(fileName, self.outFile) #download one file at a time
self.progressDialog.setLabelText("Downloading %s..." % fileName)
self.progressDialog.exec_()
def ftpCommandFinished(self, _, error):
self.setCursor(QtCore.Qt.ArrowCursor)
if self.ftp.currentCommand() == QtNetwork.QFtp.Get:
if error:
self.statusLabel.setText("Canceled download of %s." % self.outFile.fileName())
self.outFile.close()
self.outFile.remove()
else:
self.statusLabel.setText("Downloaded %s to current directory." % self.outFile.fileName())
self.outFile.close()
self.outFile = None
self.enableDownloadButton()
self.progressDialog.hide()