fork download
  1. import os, re, shutil, sys, time
  2.  
  3. if len(sys.argv) == 1 or len(sys.argv) == 2:
  4. exit()
  5. #lst_file = 'list2.txt'
  6. #search_dir = "./dir"
  7. else:
  8. lst_file = sys.argv[1]
  9. search_dir = sys.argv[2]
  10.  
  11. start = time.time()
  12. tag_dir = os.path.expandvars('$HOME') + "/Desktop/tmp/"
  13. #讀取檔案清單並存放在 dict
  14. dict = {}
  15. with open(lst_file) as f:
  16. for line in f.readlines():
  17. line = line[:-1]
  18. dict[line] = 0
  19. #print "-----print dict list-----"
  20. #print(dict.items())
  21. #print
  22. num_files = 0
  23. #測試目標目錄是否存在,不在就建立目錄
  24. if not os.path.isdir(tag_dir):
  25. os.mkdir(tag_dir)
  26. #遞迴目錄
  27. for dirPath, dirNames, fileNames in os.walk(search_dir):
  28. for f in fileNames:
  29. num_files += 1
  30. #print os.path.join(dirPath, f)
  31. #strlst = re.findall(r"\d{6,13}", f)
  32. #substr = "".join(strlst)
  33. basename = os.path.splitext(f)[0]
  34. if basename in dict:
  35. print "finded >>", os.path.join(dirPath, f)
  36. old = os.path.join(dirPath, f)
  37. new = tag_dir + f
  38. shutil.copy(old, new)
  39. print "Total search files:", num_files
  40. end = time.time()
  41. print "Elapsed time:", end - start
Success #stdin #stdout 0.01s 7620KB
stdin
Standard input is empty
stdout
Standard output is empty