12/03/2019

find pdf file (or some exe file) in directories and copy it to another directory, python sample code


import os
import glob
from shutil import copyfile


files = []
start_dir = '/Volumes/input/'
output_path = '/Volumes/output/'
pattern = "*.pdf"

total = 0
for dir,_,_ in os.walk(start_dir):
files.extend(glob.glob(os.path.join(dir,pattern)))
for i,v in enumerate(files):
#found pdf files
print(total,i,v)
#extract filename only
filename = v.split('/')[-1]
#make new filename and output path
output_filename = output_path + str(total) + '_' + filename
#if file exist? then no copy
exist = glob.glob(output_filename)
#if not copy
if len(exist) == 0:
copyfile(v, output_filename)
#print out copied filename
print('copy! : ', output_filename)
#increase global count
total += 1

No comments:

Post a Comment