import os import re from shutil import copyfile Version = "17May2017" source_dir = r'\\mount_dc-eng.sl.int\eng\Phoenix\out-temp\working' lib_dir = r'\\mount_dc-eng.sl.int\eng\R-and-D-share\Libraries\20170517' if __name__ == "__main__": try: os.mkdir(lib_dir) except: print(" ") dup_count = 0 err_count = 0 file_count = 0 # For each Directory for root, dirs, files in os.walk(source_dir): print("ROOT:", root) print("DIRS:", dirs) # Find the last segment of the path, the package name package_name = os.path.basename(root) exit # For each file in the directory for file in files: print("") print("----> ", root, file) file_count += 1 # Find the first underscore and split into segments segments = file.split('_') if len(segments) > 1: n = segments[1] # Remove the .pdf and .PDF extensions n = re.sub('.pdf', '', n) n = re.sub('.PDF', '', n) # print("Segments: ",segments[0]," <-> ", segments[1]," <-> ",n) dest_dir = os.path.join(lib_dir, n) try: # Make a directory with the doc type name os.mkdir(dest_dir) except: if os.path.exists(dest_dir): a = 1 else: print("ERROR - Could not create: ", dest_dir) dest = dest_dir srcfile = os.path.join(root, file) dest_file = os.path.join(dest_dir, package_name + '.pdf') if not os.path.exists(dest_file): try: # Copy the original srcfile into the corresponding directory copyfile(srcfile, dest_file) print("COPY FILE: ", srcfile, dest_file) except: err_count += 1 print("ERROR - Could not Copy File: ", srcfile, dest_file) else: print("WARNING - File already exists: ", dest_file) dup_count += 1 print("DONE ", " dups:", dup_count, " errs:", err_count, " files:", file_count)