我正在嘗試使用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