2016-02-19 182 views
3

我创建了一个将文件复制并移动到不同方向的程序。我认为在整个过程中添加进度条会很有趣。 我应该如何接近它?Python - 用于复制和移动文件的进度条

我的脚本做一些更认为:

排序文件没有.MDI一个文件夹中“失踪MDI”

我用distutils.dir_util.copy_tree因为shutil.copytree曾与ACCES

问题
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) 
+0

[文件复制与进度条]可能的重复(http://stackoverflow.com/questions/6044629/file-copy-with-progress-bar) –

+0

这里有很多*的答案:http:// stackoverflow。 com/questions/3160699/python-progress-bar –

回答

1

尝试用包进度

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() 
+0

以及如何实现它?我应该将它添加到现有的循环? –

+1

是的。试试这个[code](https://gist.github.com/bitf115/720dc554cf6b330ab76b) –