2013-10-06 55 views
1

我正在使用pyqt編寫應用程序。需要刪除包含子文件夾和許多文件的文件夾。文件夾的路徑位於USB磁盤中。在刪除過程中,我想向用戶顯示更新的進度欄。這裏是我試圖計算百分比,而刪除正在進行的示例代碼。帶進度的文件夾/目錄刪除

#!/usr/bin/python2.7 
import subprocess 
import os 

def progress_uninstall_dir(): 
    uninstall_path = "path/to/folder" 
    usb_mount = "path/to/usb/mount" 
    if not os.path.exists(uninstall_path): 
     print ("Directory not found.") 
    else: 
     proc=subprocess.Popen("du -ck " + usb_mount + " | grep total | cut -f 1", shell=True, stdout=subprocess.PIPE,) 
     inintial_usb_size = int(proc.communicate()[0]) 
     proc=subprocess.Popen("du -ck " + uninstall_path + " | grep total | cut -f 1", shell=True, stdout=subprocess.PIPE,) 
     folder_size_to_remove = int(proc.communicate()[0]) 
     delete_process = subprocess.Popen('rm -rf ' + uninstall_path, shell=True) 
     while delete_process.poll() is None: 
      proc=subprocess.Popen("du -ck " + usb_mount + " | grep total | cut -f 1", shell=True, stdout=subprocess.PIPE,) 
      current_size = int(proc.communicate()[0]) 
      diff_size = int(inintial_usb_size - current_size) 
      percentage = float(diff_size/folder_size_to_remove)*100 
      print (percentage) 


progress_uninstall_dir() 

但是,上面的代碼始終以0百分比表示。任何幫助,將不勝感激。

回答

0

在計算percentage時,您的問題可能是由於整數除法引起的。您現在的代碼將一個整數除以另一個整數(大概獲得值0),然後將結果轉換爲浮點數。您最好通過將兩個整數之一轉換爲在除法之前浮動:

percentage = float(diff_size)/folder_size_to_remove*100 
+0

是的。你是對的。現在代碼工作正常。 –

相關問題