简体   繁体   English

使用Python工作表中的名称映射,借助Python脚本重命名文件夹中的文件名

[英]Rename the name of files in the folder with the help of Python Script, using name map from Excel sheet

There are many CSV files in a folder which I want it to be renamed. 文件夹中有许多CSV文件,我希望将其重命名。 There is an excel sheet which contains name of files to be renamed to folder. 有一个excel表,其中包含要重命名为文件夹的文件的名称。

The files in folder are named as 文件夹中的文件命名为

TestData_30April.csv
TestData_20April.csv
TestData_18April.csv etc

while the excel sheet contains the name as 而excel表包含名称为

0.25-TestData_30April
0.98-TestData_20April
0.33-TestData_20April etc

Also first row in the excel sheet contains Header name while row 2 on wards contains the file name to be renamed. excel表中的第一行包含Header名称,而wards上的第2行包含要重命名的文件名。

My Aim is to rename 我的目标是重命名

TestData_30April.csv to 0.25-TestData_30April.csv similarly for all other files as well. TestData_30April.csv同样适用于所有其他文件的0.25-TestData_30April.csv

Here is the Code: 这是代码:

import os
import xlrd

#Excel Sheet containing name of files to be renamed in that folder
path="C:\\Users\\Desktop\\Test_Data\\Test_Summary.csv"

#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')








Here is the error
    dir_file_name = os.path.join(path_to_folder,name.split('-')[1],'.csv')

IndexError: list index out of range

try below code: 尝试以下代码:

for name in list_of_filename_in_excel:
    excel_file_name = os.path.join(path_to_folder, name,'.csv')
    newname = name
    if '-' in name:
        newname = name.split('-')[1]
    dir_file_name = os.path.join(path_to_folder,newname,'.csv' )

    if os.path.exists(dir_file_name):
        print('changing file name {} to {}'.format(newname,name))
        os.rename(dir_file_name, excel_file_name)
    else:
        print('no file {} with name found in location'.format(newname+'.csv')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Excel工作表中的名称映射,借助Python Script重命名文件夹中文件的名称 - Rename the name of files in a folder with the help of Python Script, using name map from Excel sheet 使用Excel工作表中的名称映射,借助Python Script重命名文件夹中的文件名 - Rename the file names in the folder with the help of Python Script, using name map from Excel sheet 使用 Python 重命名文件夹中的文件,使用 Excel 中的名称映射 - Rename files in a folder using Python, using name map from Excel 使用Excel重命名文件,使用Excel中的名称映射 - Rename files with Python, using name map from Excel 如何使用python从文件夹中的多个excel文件中读取具有“ mine”工作表名称的工作表? 我正在使用xlrd - how to read any sheet with the sheet name containing 'mine' from multiple excel files in a folder using python? i am using xlrd Python 脚本,用于在文件夹中搜索 excel 个文件以获取唯一字符串,然后重命名/附加文件名 - Python script to search excel files in a folder for unique strings then rename/append file name Python:使用来自列表的名称重命名文件夹中的多个文件 - Python: Rename multiple files in folder with name that come from a list 将文件重命名为文件夹名称的 Python 脚本 - Python script to rename a file to the folder name 如何使用python重命名文件夹中的图像名称? - How to rename image name in a folder using python? 如何使用Python重命名电子表格中的工作表名称? - How to rename the sheet name in the spread-sheet using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM