etl - Python Rename Filename With Substring Best Practices -
simplified, have data file:
p.a2057.aco.qassgn.d150218.t1200333.xls
i copy directory "mssp_data_archive" documentation here:
dest_dir = "c:/users/office/desktop/test/mssp_data_archive/" file in glob.glob(r'c:/users/office/desktop/test/load/*.xls'): print file shutil.copy(file, dest_dir)
i rename original version still sitting in "load" this:
qassgn.xls
i not know exact name of file monthly (appears @ least partially randomly generated elements).
i hoping substring current filename extract desired name above.
here have started:
for file in glob.glob(r'c:/users/office/desktop/test/load/*.xls'): parts = file.split('.') parts = ['c:/users/office/desktop/test/load\\p', 'a2057', 'aco', 'qassgn', 'd150218', 't1200333','xls']
i know there must better way handle os.path.splitext , os.rename avoid getting "magic numbers" trouble. not pythonic.
any pointers sincerely appreciated!
this assuming input desired name 4th section in filename. 1 thing magic number, dont know of way expect data named.
# path of files path = 'c:\\users\\office\\desktop\\test\\load' # place want output files # set input because have no idea want them dest_path = path # type of files want rename ext = r'xls' # file contain path of file file in glob.glob('{path}\\*.{ext}'.format(path=path, ext=ext)): # filename going change (dont want path @ all) name = file.split('\\')[-1] # new name of file new_file = '{path}\\{name}.{ext}'.format( path=dest_path, name=name.split('.')[3], ext=ext ) os.rename(file, new_file)
Comments
Post a Comment