Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glob
- import shutil
- import os
- #=========List of items
- list = ["cat size","house size","car size"]
- #=========directories
- FromDir = "H:/New/1RecievedFiles/"
- CopyDir = "H:/New/2CopyTo/"
- #=========get list of items to be used in glob.glob
- filesList = [x.replace(" ", "*")+"*.*" for x in list]
- #=========get list of new files in FromDir, use filesList in glob.glob
- listOfNew = [glob.glob(FromDir+item) for item in filesList ]
- # returns [['H:/New/1RecievedFiles\cat.size.txt'], ['H:/New/1RecievedFiles\house.size.fromBob.txt']]
- #=========make a single list from the list in a list
- listOfNewfiles = [item for sublist in listOfNew for item in sublist]
- # returns ['H:/New/1RecievedFiles\cat.size.txt', 'H:/New/1RecievedFiles\house.size.fromBob.txt']
- #=========copy files
- [shutil.copy2(item, CopyDir) for item in listOfNewfiles]
- $ ls -1
- copyto/
- car.size.txt
- cat.size.txt
- house.size.fromBob.txt
- import os, shutil
- # lst not list -- naming it list overshadows a builtin
- lst = ['cat size', 'house size', 'car size']
- # PEP8 - lowercase with underscore here; CamelCase for classes
- from_dir = os.path.abspath(os.path.expanduser('~/test'))
- copy_dir = os.path.abspath(os.path.expanduser('~/test/copyto'))
- to_copy = [f for f in os.listdir(from_dir) if os.path.isfile(f)]
- # ['car.size.txt', 'house.size.fromBob.txt', 'cat.size.txt']
- for filename in to_copy:
- needle = ' '.join(filename.split('.')[:2])
- if needle in lst:
- dest_dir = os.path.join(copy_dir, needle)
- os.mkdir(dest_dir)
- shutil.copy2(os.path.join(from_dir, filename), dest_dir)
- copyto:
- car size/
- cat size/
- house size/
- copyto/car size:
- car.size.txt
- copyto/cat size:
- cat.size.txt
- copyto/house size:
- house.size.fromBob.txt
- import os,shutil
- for item in list:
- if 'cat' in listitem:
- shutil.copyfile(source, 'H:/new/2CopyTo/cat size/')
- elif 'house' in listitem:
- shutil.copyfile(source, 'H.......etc etc
- elif 'car'.....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement