Python - 复制和移动文件的进度条

Python - progress bar for copying and moving files

我创建了一个程序,可以将文件复制和移动到不同的方向。我在想如果给整个过程加上一个进度条会很有趣。 我应该如何处理它?

我的脚本做了一些思考:

将没有 .mdi 文件的文件排序到文件夹 "missing mdi"

我使用 distutils.dir_util.copy_tree 因为 shutil.copytree 访问有问题

src = raw_input("Enter source disk location: ")
src = os.path.abspath(src)
dst = raw_input("Enter first destination : ")
dst = os.path.abspath(dst)
dest = raw_input("Enter second destination : ")
dest = os.path.abspath(dest)

for dir, dirs, files in os.walk(src):
if any(f.endswith('.mdi') for f in files):
    dirs[:] = []  # do not recurse into subdirectories
    continue      # ignore this directory

# do something with the files here, there are no .txt files.
files = [os.path.join(dir, f) for f in files]
print "files -->", files

for list in files:


    print "list --->", list
    #---------parameters-------------------#
    part1 = os.path.dirname(list)
    print "part1" ,part1
    part2 = os.path.dirname(os.path.dirname(part1))
    print "part2" ,part2
    part3 = os.path.split(part1)[1]
    print "part 3 ->",part3
    path_miss1 = os.path.join(dst, "missing_mdi")
    print "path_miss1", path_miss1
    #---------first location-------------------#
    path_miss = os.path.join(path_miss1, part3)
    print "path_miss", path_miss
    #---------second location-------------------#
    path_missing = os.path.join(dest, "missing_mdi")
    print "path_missing", path_missing


    try:
        #---------first location-------------------#
        if not os.path.exists(path_miss):
            os.makedirs(path_miss)
        else:
            pass

        if os.path.exists(path_miss):
            distutils.dir_util.copy_tree(part1,path_miss)
        else:
            print "missing_file"

        if(get_size(path_miss)) == 0:
            os.rmdir(path_miss)
        else:
            pass

        #---------second location-------------------#
        if not os.path.exists(path_missing):
            os.makedirs(path_missing)
        else:
            pass

        if os.path.exists(path_missing):
            shutil.move(part1,path_missing)
        else:
            print "missing_file"

        if(get_size(path_missing)) == 0:
            os.rmdir(path_missing)
        else:
            pass

    except Exception, l:
        print "l --->",str  ( l )

尝试使用包 progressbar

from progressbar import ProgressBar, Percentage, Bar, ETA
from time import sleep


progress, progress_maxval = 0, 10
pbar = ProgressBar(widgets=['Progress ', Percentage(), Bar(), ' ', ETA(), ],
                   maxval=progress_maxval).start()

for i in xrange(progress_maxval):
    progress += 1
    sleep(1)
    pbar.update(progress)

pbar.finish()