Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文件夹/目录随进度删除_Python_Python 2.7_Directory_Progress - Fatal编程技术网

Python 文件夹/目录随进度删除

Python 文件夹/目录随进度删除,python,python-2.7,directory,progress,Python,Python 2.7,Directory,Progress,我正在使用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"

我正在使用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 = float(diff_size)/folder_size_to_remove*100