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
百分比表示。任何幫助,將不勝感激。
是的。你是對的。現在代碼工作正常。 –