Python 基于索引更改多个文件的名称
我目前有几百个pdf文件,文件名我想更改 文件的当前名称实际上并不遵循模式,但是我有一个Excel文件,其中包含当前文件名以及我想要的特定文件的新文件名。这看起来类似于: 我正在寻找一种在python中根据我的excel索引重命名所有文件(大约500个)的方法 我尝试的是:Python 基于索引更改多个文件的名称,python,Python,我目前有几百个pdf文件,文件名我想更改 文件的当前名称实际上并不遵循模式,但是我有一个Excel文件,其中包含当前文件名以及我想要的特定文件的新文件名。这看起来类似于: 我正在寻找一种在python中根据我的excel索引重命名所有文件(大约500个)的方法 我尝试的是: import os path = 'C:\\Users\\Desktop\\Project\\' files = os.listdir(path) for file in files: os.rename(os.
import os
path = 'C:\\Users\\Desktop\\Project\\'
files = os.listdir(path)
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, '00' + file + '.pdf'))
谢谢。如果您有带名称的表,您可以使用以下代码:
import os
names = '''a.pdf 001.pdf
b.pdf 002.pdf
c.pdf 003.pdf'''
os.chdir(r'C:\Users\Desktop\Project')
for line in names.splitlines(False):
old, new = line.split()
os.rename(old, new)
您可以将表格从Excel复制到这段代码中
若你们不在乎桌子,你们可以试试
import os
from itertools import count
numbers = count(1)
os.chdir(r'C:\Users\Desktop\Project')
for old in os.listdir('.'):
if not old.endswith('.pdf'):
continue
new = '%03d.pdf' % next(numbers)
os.rename(old, new)
如果表具有名称,则可以使用以下代码:
import os
names = '''a.pdf 001.pdf
b.pdf 002.pdf
c.pdf 003.pdf'''
os.chdir(r'C:\Users\Desktop\Project')
for line in names.splitlines(False):
old, new = line.split()
os.rename(old, new)
您可以将表格从Excel复制到这段代码中
若你们不在乎桌子,你们可以试试
import os
from itertools import count
numbers = count(1)
os.chdir(r'C:\Users\Desktop\Project')
for old in os.listdir('.'):
if not old.endswith('.pdf'):
continue
new = '%03d.pdf' % next(numbers)
os.rename(old, new)
如果可以将excel文件另存为csv,则应该可以使用
导入操作系统
导入csv
路径='C:\\Users\\Desktop\\Project\\'
将open('my_csv.csv')作为f:
读卡器=csv。读卡器(f)
下一个(读卡器)#跳过第一行
对于行内读取器:
src=os.path.join(路径,第[0]行)
dest=os.path.join(路径,第[1]行)
重命名操作系统(src,dest)
如果您可以将excel文件另存为csv,则应该可以使用
导入操作系统
导入csv
路径='C:\\Users\\Desktop\\Project\\'
将open('my_csv.csv')作为f:
读卡器=csv。读卡器(f)
下一个(读卡器)#跳过第一行
对于行内读取器:
src=os.path.join(路径,第[0]行)
dest=os.path.join(路径,第[1]行)
重命名操作系统(src,dest)
你真的很接近
您需要迭代xlsx
文件中的名称。一种简单的方法是使用加载数据,最后迭代源
和dest
列并重命名文件
可以使用从给定文件夹和给定文件创建完整路径
代码如下:
# Import module
import os # Rename file
import pandas as pd # read csv
# Your different folders
path_folder = r'C:\Users\Desktop\Project'
path_csv = r'C:\Users\Desktop\Project\csv_file.xlsx'
# Load data
df = pd.read_excel(path_csv)
print(df)
# Current file name Desired file name
# 0 a.pdf 001.pdf
# 1 b.pdf 002.pdf
# 2 c.pdf 003.pdf
# Iterate over each row of the dataframe
for old_name, new_name in zip(df["Current file name"], df["Desired file name"]):
# Create source path and destination path
source_file = os.path.join(path_folder, old_name)
dest_file = os.path.join(path_folder, new_name)
# Rename the current file using the source path (old name)
# and the destination path (new name)
os.rename(source_file, dest_file )
使用的Excel文件:
希望有帮助 你真的很接近
您需要迭代xlsx
文件中的名称。一种简单的方法是使用加载数据,最后迭代源
和dest
列并重命名文件
可以使用从给定文件夹和给定文件创建完整路径
代码如下:
# Import module
import os # Rename file
import pandas as pd # read csv
# Your different folders
path_folder = r'C:\Users\Desktop\Project'
path_csv = r'C:\Users\Desktop\Project\csv_file.xlsx'
# Load data
df = pd.read_excel(path_csv)
print(df)
# Current file name Desired file name
# 0 a.pdf 001.pdf
# 1 b.pdf 002.pdf
# 2 c.pdf 003.pdf
# Iterate over each row of the dataframe
for old_name, new_name in zip(df["Current file name"], df["Desired file name"]):
# Create source path and destination path
source_file = os.path.join(path_folder, old_name)
dest_file = os.path.join(path_folder, new_name)
# Rename the current file using the source path (old name)
# and the destination path (new name)
os.rename(source_file, dest_file )
使用的Excel文件:
希望有帮助 你试过什么?显示一些代码。导入os path='C:\\Users\\Desktop\\Project\\'files=os.listdir(路径)文件中的文件:os.rename(os.path.join(路径,文件),os.path.join(路径,'00'+文件+'.pdf')),但通常情况下:将excel文件的内容作为字典加载(如果将表保存为csv,则无需额外的LIB即可轻松加载)或者甚至是成对的,然后检查所有内容并尝试重命名(我说“尝试”,因为最好用
try
)包装它。您尝试过什么?显示一些代码。导入os path='C:\\Users\\Desktop\\Project\\'files=os.listdir(路径)文件中的文件:os.rename(os.path.join(路径,文件),os.path.join(路径,'00'+文件+'.pdf')),但通常情况下:将excel文件的内容作为字典加载(如果将表保存为csv,则无需额外的LIB即可轻松加载)或者甚至只是配对,然后遍历所有内容并尝试重命名(我说“try”,因为最好用try
包装它)。