2016-12-27 64 views
0

我正在嘗試使用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() 
+0

我需要更多的代碼來告訴。 'self.progressDialog.exec _()'應該是一個阻塞模態對話框。它看起來像ftp get是非阻塞的,所以你必須等到使用commandFinished()信號完成下載。覆蓋outFile變量可能會導致問題。 http://pyside.github.io/docs/pyside/PySide/QtNetwork/QFtp.html#PySide.QtNetwork.PySide.QtNetwork.QFtp.get – HashSplat

+0

@HashSplat,我用:self.progressDialog = QtGui.QProgressDialog(self) 。 – Curiosity

+0

@HashSplat基本上,我試圖創建一個單一的點擊下載所有基於[此版本](http://stackoverflow.com/questions/1995046/creating-an-ftp-client-with-python) – Curiosity

回答

0

self.progressDialog.exec_()應該是一個阻塞模態對話框。使用self.progressDialog.show()進行非阻塞呼叫。

它看起來像ftp get是非阻塞的,所以你必須等到使用commandFinished()信號完成下載。

我的猜測是循環中的每個迭代都覆蓋self.outFile,因此沒有任何對該對象的Python引用。這使得對象在Python執行垃圾回收時死亡。我的猜測是,你的前兩個文件小而快,你的第三個文件更大,所以其他文件能夠在垃圾回收之前下載。對於最後一個文件,這個或垃圾收集只是更快。

http://pyside.github.io/docs/pyside/PySide/QtNetwork/QFtp.html#PySide.QtNetwork.PySide.QtNetwork.QFtp.get

class FtpWindow(QtGui.QDialog): 

    def __init__(self, parent=None): 
     self.fileList = QtGui.QTreeWidget() 
     self.ftp = QtNetwork.QFtp(self) 
     self.progressDialog = QtGui.QProgressDialog(self) 
     self.progressDialog.canceled.connect(self.ftp.abort) 
     self.downloadAllButton.clicked.connect(self.downloadAllFile) 
     self.ref_holder = {} 
     self.ftp.commandFinished.connect(self.ftpCommandFinished) 

    def download_file(self, filename): 
     """Non blocking start downloading a file.""" 
     outFile = QtCore.QFile(filename) 
     cmd_id = self.ftp.get(filename, outFile) # Non blocking just start downloading 

     # This keeps the object alive and doesn't overwrite them. 
     self.ref_holder[cmd_id] = [filename, outFile] 

    def downloadAllFile(self): 
     self.progressDialog.reset() 
     num_downloads = self.fileList.topLevelItemCount() 
     self.progressDialog.setMaximum(num_downloads) 
     self.progressDialog.setValue(0) 
     self.progressDialog.setLabelText("Downloading %d files ..." % num_downloads) 
     self.progressDialog.show() 
     for jj in range(num_downloads): # how many files in a particular folder 
      fileName = self.fileList.topLevelItem(jj).text(0) 
      self.download_file(fileName) # Non blocking, and doesn't overwrite self.outFile with every iteration 

    def ftpCommandFinished(self, cmd_id, error=None): 
     """Increased the number of items finished.""" 
     self.progressDialog.setValue(self.progressDialog.value()+1) 
     item = self.ref_holder.pop(cmd_id) # Remove the reference for the finished item 
     if error: 
      self.progressDialog.setLabelText("Error downloading %s" % item[0]) 

     # Check if all downloads are done 
     if len(self.ref_holder) == 0: 
      self.progressDialog.setValue(self.progressDialog.maximium()) 
      self.progressDialog.close() # This shouldn't be needed 

我的例子,直到命令完成上述將持有的文件名和不過outFile對象引用。當命令完成時,引用被刪除,允許python清理對象。

+0

文件感謝很多爲您的代碼,它的作品!但是,我發現即使處理下載,文件也不會顯示在本地目標中。我必須添加「self.outFile.open(QtCore.QIODevice.WriteOnly)」來啓用本地下載和保存。爲什麼?我已經包含我的版本 – Curiosity

0

感謝HashSplat的輸入。我有幾個更新,使其功能齊全:

class FtpWindow(QtGui.QDialog): 

def __init__(self, parent=None): 
    self.fileList = QtGui.QTreeWidget() 
    self.ftp = QtNetwork.QFtp(self) 
    self.progressDialog = QtGui.QProgressDialog(self) 
    self.progressDialog.canceled.connect(self.ftp.abort) 
    self.downloadAllButton.clicked.connect(self.downloadAllFile) 
    self.ref_holder = {} 
    self.ftp.commandFinished.connect(self.ftpCommandFinished) 

def download_file(self, fileName): 
    """Non blocking start downloading a file.""" 
    self.outFile = QtCore.QFile(fileName) 

    """ Need this to have files saved locally """ 
    if not self.outFile.open(QtCore.QIODevice.WriteOnly): 
     QtGui.QMessageBox.information(self, "FTP", 
       "Unable to save the file %s." % fileName) 
     self.outFile = None 
     return 
    cmd_id = self.ftp.get(filename, self.outFile) # Non blocking just start downloading 

    # This keeps the object alive and doesn't overwrite them. 
    self.ref_holder[cmd_id] = [filename, self.outFile] 

def downloadAllFile(self): 
    self.progressDialog.reset() 
    self.num_downloads = self.fileList.topLevelItemCount() 
    self.counter=1 
    self.progressDialog.setLabelText("Downloading %d/%d files ..." % (self.counter, self.num_downloads)) 
    self.progressDialog.show() 
    for jj in range(num_downloads): # how many files in a particular folder 
     fileName = self.fileList.topLevelItem(jj).text(0) 
     self.download_file(fileName) # Non blocking, and doesn't overwrite self.outFile with every iteration 

def ftpCommandFinished(self, cmd_id, error=None): 
    """Increased the number of items finished.""" 
    self.progressDialog.setValue(self.progressDialog.value()+1) 
    item = self.ref_holder.pop(cmd_id) # Remove the reference for the finished item 
    if error: 
     self.progressDialog.setLabelText("Error downloading %s" % item[0]) 

    # Check if all downloads are done 
    if len(self.ref_holder) == 0: 
     self.progressDialog.close() # This closes the extra window 
     self.outFile.close() # You need this to have the last file saved 
    else: 
     self.counter+=1 
     self.progressDialog.setLabelText("Downloading %d/%d files ..." % (self.counter, self.num_downloads)) 

def updateDataTransferProgress(self, readBytes, totalBytes): 
    self.progressDialog.setMaximum(totalBytes) 
    self.progressDialog.setValue(readBytes) 
+0

在ftpCommandFinished where self.outFile.close()#你需要這個保存最後一個文件。您應該在if語句之前移動它,並關閉每個outFile。 'item [1] .close()#關閉outFile''if len(self.ref_holder)== 0:...' – HashSplat