python批量修改文件名为excel中指定名称_在Python脚本的帮助下,使用excelsh中的名称映射重命名文件夹中的文件名...
在一個文件夾中有許多CSV文件我想重命名。有一個excel工作表,其中包含要重命名為文件夾的文件名。在
文件夾中的文件名為
測試數(shù)據(jù)?30四月.csv在
測試數(shù)據(jù)?20四月.csv在
測試數(shù)據(jù)18四月.csv等等
而excel工作表包含的名稱為
0.25-測試數(shù)據(jù)4月30日
0.98-4月20日測試數(shù)據(jù)
0.33-4月20日測試數(shù)據(jù)等
我的目標是改名
“測試數(shù)據(jù)\u 30四月.csv“至”0.25-TestData_30四月.csv“其他文件也一樣。在
這是我寫的代碼(它不能按預(yù)期工作)import os
import xlrd
#Excel Sheet containing name of files to be renamed in that folder
path="C:\\Users\\Desktop\\Test_Data\\Test_Summary.xlsx"
#Folder Containg all orginal file names
dir = "C:\\Users\\Desktop\\Wear_Data"
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
#In excel sheet column X or col_values(23) contains the file name to be renamed
print(sheet.col_values(23))
list_of_filename_in_folder = [] # name of the files in the folder
list_of_filename_in_excel = [] #name of the files in excel
path_to_folder = '' # base path of folder
for name in list_of_filename_in_excel:
excel_file_name = os.path.join(path_to_folder, name,'.csv')
dir_file_name = os.path.join(path_to_folder,name.split('-')[1],'.csv' )
if os.path.exists(dir_file_name):
print('changing file name {} to {}'.format(name.split('-')[1],name))
os.rename(dir_file_name, excel_file_name)
else:
print('no file {} with name found in location'.format(name.split('-')[1]+'.csv')
總結(jié)
以上是生活随笔為你收集整理的python批量修改文件名为excel中指定名称_在Python脚本的帮助下,使用excelsh中的名称映射重命名文件夹中的文件名...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: it计算机哪些专业术语,IT之家学院:笔
- 下一篇: termux php 出错,androi