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

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");



留言

這個網誌中的熱門文章

Easy way for Try out o365 E3 - Step2 - Install Azure AD connect sync AD account to o365

AHV - migrate all VM on one node to other node

Win7 RSAT & 2003R2 ADWS