Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import glob
  2. import shutil
  3. import os
  4.  
  5. #=========List of items
  6. list = ["cat size","house size","car size"]
  7.  
  8. #=========directories
  9. FromDir = "H:/New/1RecievedFiles/"
  10. CopyDir = "H:/New/2CopyTo/"
  11.  
  12. #=========get list of items to be used in glob.glob
  13. filesList = [x.replace(" ", "*")+"*.*" for x in list]
  14.  
  15. #=========get list of new files in FromDir, use filesList in glob.glob
  16. listOfNew = [glob.glob(FromDir+item) for item in filesList ]
  17. # returns [['H:/New/1RecievedFiles\cat.size.txt'], ['H:/New/1RecievedFiles\house.size.fromBob.txt']]
  18.  
  19. #=========make a single list from the list in a list
  20. listOfNewfiles = [item for sublist in listOfNew for item in sublist]
  21. # returns ['H:/New/1RecievedFiles\cat.size.txt', 'H:/New/1RecievedFiles\house.size.fromBob.txt']
  22.  
  23. #=========copy files
  24. [shutil.copy2(item, CopyDir) for item in listOfNewfiles]
  25.  
  26. $ ls -1
  27. copyto/
  28. car.size.txt
  29. cat.size.txt
  30. house.size.fromBob.txt
  31.  
  32. import os, shutil
  33. # lst not list -- naming it list overshadows a builtin
  34. lst = ['cat size', 'house size', 'car size']
  35. # PEP8 - lowercase with underscore here; CamelCase for classes
  36. from_dir = os.path.abspath(os.path.expanduser('~/test'))
  37. copy_dir = os.path.abspath(os.path.expanduser('~/test/copyto'))
  38.  
  39. to_copy = [f for f in os.listdir(from_dir) if os.path.isfile(f)]
  40. # ['car.size.txt', 'house.size.fromBob.txt', 'cat.size.txt']
  41.  
  42. for filename in to_copy:
  43. needle = ' '.join(filename.split('.')[:2])
  44. if needle in lst:
  45. dest_dir = os.path.join(copy_dir, needle)
  46. os.mkdir(dest_dir)
  47. shutil.copy2(os.path.join(from_dir, filename), dest_dir)
  48.  
  49. copyto:
  50. car size/
  51. cat size/
  52. house size/
  53.  
  54. copyto/car size:
  55. car.size.txt
  56.  
  57. copyto/cat size:
  58. cat.size.txt
  59.  
  60. copyto/house size:
  61. house.size.fromBob.txt
  62.  
  63. import os,shutil
  64.  
  65. for item in list:
  66. if 'cat' in listitem:
  67. shutil.copyfile(source, 'H:/new/2CopyTo/cat size/')
  68. elif 'house' in listitem:
  69. shutil.copyfile(source, 'H.......etc etc
  70. elif 'car'.....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement