1 回答
TA贡献1862条经验 获得超7个赞
您可以使用zip迭代相应的项目对:
import os
for old, new in zip(df['Filename_Old'], df['Filename_New']):
os.rename(old, new)
当您批量处理大量文件时,可能还值得这样做,try以便如果其中一个重命名失败(例如其中一个文件已被重命名),它不会停止整个操作。例如:
for old, new in zip(df['Filename_Old'], df['Filename_New']):
try:
os.rename(old, new)
except OSError as exc:
print(f'WARNING: could not rename {old} to {new}: {exc}')
else:
print(f'renamed {old} to {new}')
请注意,我认为您的问题是指您的 CSV 文件的原始内容如下所示:
Filename_Old,Filename_New
Jean1_Aspect_Six.jpg,Jean1_Aspect_Six_New.jpg
Jean2_Aspect_Five.jpg,Jean2_Aspect_Ratio_TN.jpg
Jean1_Table_P2.jpg,Jean1_Table_TN.jpg
Jas_Snail_P3.png,Jas_P3_TN.png
并且您已阅读并使用:
import pandas as pd
df = pd.read_csv("your_file.csv")
添加回答
举报