Reputation: 309
I'm quite new to Python. I've been exploring the shutil
module and can move things in general. My question revolves around:
Imagine a scenario in which you have hundreds of files in an export folder. While all the files are distinct, 13 of each are for a specific vendor. I would like to create a script that goes through the export folder, evaluates each file name, grabs all the Apple files and puts them in the Apple Folder, Intel files and puts them in the Intel Folder, etc. Any wisdom would be greatly appreciated.
I was trying to have wildcards in the shutil
copy, but did not have any luck.
Thanks,
JT
Upvotes: 18
Views: 68835
Reputation: 116
The code below is working for me -
import os
import shutil
ent_dir_path = input("Enter the path of the directory:") #source directory
out_dir_path = input("Enter the path of the directory where you want to move all these files:") #destination directory
if not os.path.exists(out_dir_path): #create folder if doesn't exist
os.makedirs(out_dir_path)
file_type = input("Enter the first few characters of type of file you want to move:")
entries = os.listdir(ent_dir_path)
for entry in entries:
if entry.startswith(file_type):
#print(ent_dir_path + entry)
shutil.move(ent_dir_path + entry, out_dir_path)
# ent_dir_path + entry is the path of the file you want to move
# example- entry = Apple.txt
# ent_dir_path = /home/user/Downloads/
# ent_dir_path + entry = /home/user/Downloads/Apple.txt (path of the
# file)
Upvotes: 1
Reputation: 564
Easiest solution I could think of:
import shutil
import os
source = '/path/to/source_folder'
dest1 = '/path/to/apple_folder'
dest2 = '/path/to/intel_folder'
files = os.listdir(source)
for f in files:
if (f.startswith("Apple") or f.startswith("apple")):
shutil.move(f, dest1)
elif (f.startswith("Intel") or f.startswith("intel")):
shutil.move(f, dest2)
The destination folders do need to exist.
Upvotes: 24
Reputation: 293
Minor edit on Padaic's answer:
import glob
import shutil
vendors = ("*Apple*.*", "*Intel*.*")
paths = (".\\Apple\\", ".\\Intel\\")
for idx in range(len(vendors)):
for matches in glob.glob(vendors[idx]):
shutil.move(matches, paths[idx])
Upvotes: 1
Reputation:
Andreas's answer is fine for the Apples example you've given. If you're looking for something more meaty, check out regexs. They let you search for repeating patterns of text.
Where I work, for example, we have different formats of text depending on whether the file is a report, or a technical drawing etc. They all follow a format similar to "company initials/project number/department.
As a regex, you could search something like:
import re
fileregex = re.compile(r'(\w\w)/(\d{4})/(\w\w))
for file in os.listdir(*path*):
filename = fileregex.search(file)
It's a handy tool to know.
Upvotes: 0
Reputation: 180522
import glob, shutil
for file in glob.glob('path_to_dir/apple*'):
shutil.move(file, new_dst)
# a list of file types
vendors =['path_to_dir/apple*', 'path_to_dir/intel*']
for file in vendors:
for f in (glob.glob(file)):
if "apple" in f: # if apple in name, move to new apple dir
shutil.move(f, new_apple_dir)
else:
shutil.move(f, new_intel_dir) # else move to intel dir
Upvotes: 3
Reputation: 2190
Assuming there are specific strings in the file names that identify which vendor the report relates to, you could create a dictionary that maps those identifying-strings to the appropriate vendor. For example:
import shutil
import os
path = '/path/to/location'
vendorMap = {'apple': 'Apple',
'intel': 'Intel',
'stringID3': 'vendor3'}
files = os.listdir(path)
for f in files:
for key, value in vendorMap.iteritems():
if key in f.lower():
shutil.copy(f, path + '/' + value)
else:
print 'Error identifying vendor for', f
This will create a folder in the current directory, named for the appropriate vendor, and copy that vendor's reports there. Note that this example uses the s.lower() method so that it won't matter whether the vendor name is capitalized.
Upvotes: 2