我正在尝试使用for-loop从ftp站点下载多个文件。在弹出python.exe关闭窗口之前,下面的代码似乎只适用于循环中的前两个文件。两个下载的文件是完美的,但第三个下载的文件在关机时是空的。我没有得到其余的文件。任何想法可能是什么问题?为什么QtNetwork.QFtp.get下载在多个文件的for循环中失败?
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()
我需要更多的代码来告诉。 'self.progressDialog.exec _()'应该是一个阻塞模态对话框。它看起来像ftp get是非阻塞的,所以你必须等到使用commandFinished()信号完成下载。覆盖outFile变量可能会导致问题。 http://pyside.github.io/docs/pyside/PySide/QtNetwork/QFtp.html#PySide.QtNetwork.PySide.QtNetwork.QFtp.get – HashSplat
@HashSplat,我用:self.progressDialog = QtGui.QProgressDialog(self) 。 – Curiosity
@HashSplat基本上,我试图创建一个单一的点击下载所有基于[此版本](http://stackoverflow.com/questions/1995046/creating-an-ftp-client-with-python) – Curiosity