使用csv数据批量重命名图片

时间:2018-12-31 18:08:24

标签: windows python-2.7 csv file-rename

Windows错误:从csv生成的列表重命名图像文件时,出现[错误3]。

(我想将csv信息添加到其配对图像中)

我尝试将文件名分开,然后在for循环中使用os.rename将CSV文件信息添加到开头。

import os
from os import path
import csv

root_path = 'C:\Users\e150005\Desktop\excel test\Structures'
image_source = 'C:\Users\e150005\Desktop\excel test_test\photos'
##--------------------------------------------------------
##https://stackoverflow.com/questions/19486369/extract-csv-file-specific- 
  columns-to-list-in-python

## loads specific columns from .csv file and saves as list

with open('exif_output.csv', 'rU') as infile:
    reader = csv.DictReader(infile)
    data = {}
    for row in reader:
        for header, value in row.items():
            try:
                data[header].append(value)
            except KeyError:
                    data[header] = [value]

file_name = data['FileName']
pole_tag = str(data['pole tag'])
print pole_tag


##----------------------------------------------------------
##https://www.youtube.com/watch?v=ve2pmm5JqmI
## renames DJI_0001 to XX_DJI_0001

os.chdir('C:\Users\e150005\Desktop\excel test\photos')
print(os.getcwd())


for f in os.listdir('C:\Users\e150005\Desktop\excel test\photos'):
    file_name, file_ext = os.path.splitext(f)
    #print file_name
    os.rename(pole_tag,f)

我希望输出将DJI_0001更改为01_DJI_0001,其中前导01来自csv文件。

0 个答案:

没有答案