python - Compare & Change File Name
In response to work needs
write a python script for compare & change file name from excel file
the script will compare field "servicetag" data in sheet "host" & file name
Reference file (demo.xlsx)formate
code
write a python script for compare & change file name from excel file
the script will compare field "servicetag" data in sheet "host" & file name
Reference file (demo.xlsx)formate
code
import os, re , pandas # rename file countcount =0 # get script root pathdesdir=os.getcwd() # get xlsx file pathdatafile = input("xlsx file path\n") #load xlsx file datadf = pandas.read_excel(datafile,sheet_name='host') df_df=pandas.DataFrame(df) for i in range(len(df_df)): for file in os.listdir(desdir): if bool(re.search(df_df['servicetag'][i], file)): #file name check if bool(re.search(df_df['host'][i], file)): break; else: count =count+1 os.rename(os.path.join(desdir, file), os.path.join(desdir, df_df['host'][i]+'-'+file)) # file rename print(os.path.join(desdir, df_df['host'][i]+'-'+file)) print("rename",count,"file"); os.system("pause");
留言
張貼留言