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)
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
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
我能想到的最简单的解决方案是:
目标文件夹需要存在。
假设文件名中有特定的字符串用于标识报表所涉及的供应商,则可以创建一个字典,将这些标识字符串映射到相应的供应商。例如:
这将在当前目录中创建一个为相应供应商命名的文件夹,并将该供应商的报告复制到那里。注意,这个例子使用s.lower()方法,这样供应商名称是否大写就不重要了。
相关问题 更多 >
编程相关推荐