I am trying to download multiple files from a ftp site using a for-loop. The following code seems to work only for the first 2 files in the loop before the python.exe shutdown window pops up. Two downloaded files are perfect, but the 3rd downloaded file is empty upon the shutdown. I don't get the rest of files. Any idea what could be the problem?
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()
Thanks for HashSplat's input. I have a few updates to make it fully functional: