使用Pandas重命名文件

时间:2015-09-11 14:17:45

标签: python pandas

我尝试使用Excel工作表和pandas重命名文件,但我一直收到IO错误。在'文件名'我将原始文件名以及我想要的名称重命名为'重命名'柱。我知道还有其他方法可以做到这一点,但我不明白为什么下面的方法不起作用。

import os
import pandas as pd
from os.path import join
import shutil

dir = os.path.dirname(__file__)
excelFile = join(dir,'test.xlsx')
output_image_dir = os.path.join(dir,'PREAPPROVAL')  
df = pd.read_excel(excelFile, sheetname='rename')


for x,y in zip(df['Filename'].astype('str'),df['rename']):
    x = join(output_image_dir,x)
    y = join(output_image_dir,y)
    shutil.move(x,y)

Traceback (most recent call last):
  File "D:\Dropbox\1. Projects\2. Python\2. X1\rename_images.py", line 26, in <module>
    shutil.move(x,y)
  File "c:\python27\lib\shutil.py", line 302, in move
    copy2(src, real_dst)
  File "c:\python27\lib\shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "c:\python27\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'D:\\Dropbox\\1. Projects\\2. Python\\2. X1\\PREAPPROVAL\\VERT.jpg'
[Finished in 0.3s]

1 个答案:

答案 0 :(得分:0)

不好意思,但我明白了。其中一个文件名中有一个额外的空间可以将所有内容都丢掉。感谢。