我正在尝试使用for循环从ftp站点下载多个文件。下面的代码似乎只适用于python.exe关闭窗口弹出。两个下载的文件是完美的,但是第三个下载的文件在关机时是空的。剩下的文件我都拿不到了。知道有什么问题吗?你知道吗
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()
感谢HashSplat的输入。我有一些更新,使其功能齐全:
self.progressDialog.exec_()
应该是一个阻塞模式对话框。对非阻塞调用使用self.progressDialog.show()
。你知道吗看起来ftp get是非阻塞的,所以您必须使用commandFinished()信号等待下载完成。你知道吗
我猜循环中的每个迭代都会覆盖自输出文件,因此没有任何python对该对象的引用。这使得每当python执行垃圾收集时,对象都会死亡。我的猜测是,你的前两个文件小而快,你的第三个文件更大,所以其他文件可以在垃圾收集前下载。对于最后一个文件来说,这或垃圾收集都只是更快。你知道吗
http://pyside.github.io/docs/pyside/PySide/QtNetwork/QFtp.html#PySide.QtNetwork.PySide.QtNetwork.QFtp.get
上面的示例将保留filename和outFile对象引用,直到命令完成。命令完成后,引用将被删除,从而允许python清理对象。你知道吗
相关问题 更多 >
编程相关推荐