1 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用zip迭代相應(yīng)的項(xiàng)目對(duì):
import os
for old, new in zip(df['Filename_Old'], df['Filename_New']):
os.rename(old, new)
當(dāng)您批量處理大量文件時(shí),可能還值得這樣做,try以便如果其中一個(gè)重命名失?。ɡ缙渲幸粋€(gè)文件已被重命名),它不會(huì)停止整個(gè)操作。例如:
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}')
請(qǐng)注意,我認(rèn)為您的問(wèn)題是指您的 CSV 文件的原始內(nèi)容如下所示:
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")
添加回答
舉報(bào)