【问题标题】:Duplicating files and renaming them based on a CSV using python使用python复制文件并根据CSV重命名它们
【发布时间】:2019-01-17 09:20:57
【问题描述】:

我有大约 400 个文件需要根据 csv 文件复制和重命名,其中一些文件将被复制多次并给出不同的名称。

我有 CSV 文件,在 A 列中它具有原始名称,在 B 列中具有新名称。

IE 1000.jpg 1000 - 10x10.jpg 1000.jpg 1000 - 12x12.jpg

我编写的 python 脚本将复制文件、重命名并移动,但只移动一次。所以如果我需要 4 份 1000.jpg 重命名,我只会得到一份。

我对此仍然非常陌生,因此非常感谢任何帮助。

import os
import csv
import shutil
# open and store the csv file
IDs = {}
with open('old-names-new-names.csv','rb') as csvfile:
    timeReader = csv.reader(csvfile, delimiter = ',')
    # build dictionary with associated IDs
    for row in timeReader:
        IDs[row[0]] = row[1]
# move files
path = '/start_location/'
tmpPath = '/save_location/'
for oldname in os.listdir(path):
    # ignore files in path which aren't in the csv file
    if oldname in IDs:
        try:
            shutil.copy(os.path.join(path, oldname), os.path.join(tmpPath, IDs[oldname]))
        except:
            print 'File ' + oldname + ' could not be renamed to ' + IDs[oldname] + '!'

【问题讨论】:

    标签: python copy rename


    【解决方案1】:

    如果您是 Python 新手,我建议您使用 Pandas 来解决这个问题。

    以下是我的设置方式。

    假设您的csv 文件包含old_namenew_name 列。

    import pandas as pd 
    name_map = pd.read_csv('old-names-new-names.csv')
    name_map.head()
    
        new_name    old_name
    0   new_33.txt  old_1.txt
    1   new_18.txt  old_2.txt
    2   new_29.txt  old_3.txt
    3   new_31.txt  old_4.txt
    4   new_64.txt  old_1.txt
    

    start_location 我们有以下文件:

    import os 
    os.listdir('start_location')
    ['old_1.txt', 'old_4.txt', 'old_2.txt', 'old_3.txt']
    

    我们的save_location 目录是空的。

    要复制文件,我们可以像这样对 Pandas 数据框进行快速循环,使用shutil 将文件复制到名称来自new_name 列的新目录。

    import shutil 
    for i, r in name_map.iterrows():
        shutil.copy('start_location/{}'.format(r.old_name), 'save_location/{}'.format(r.new_name))
    

    当我们检查目标目录时,我们看到所有内容都在那里:

    os.listdir('save_location')
    
    ['new_60.txt',
     'new_29.txt',
     'new_31.txt',
     'new_64.txt',
     'new_48.txt',
     'new_33.txt',
     'new_96.txt',
     'new_18.txt']
    

    如果您不想使用 pandas,请考虑以下选项:

    import os
    import csv
    import shutil
    with open('old-names-new-names.csv','rt') as csvfile:
        timeReader = csv.reader(csvfile, delimiter = ',')
        i = 0 
        for row in timeReader:
            if i > 0:
                start_loc = row[1]
                save_loc = row[0]
                shutil.copy('start_location/{}'.format(start_loc), 'save_location/{}'.format(save_loc))
            i+=1
    

    【讨论】:

    • 感谢您的帮助,但我在将 Pandas 安装到我的 Mac 上时遇到问题,您可以建议一种不包括安装它的方法。
    • 非常感谢!
    【解决方案2】:

    您只会得到一份副本,因为您使用的是使用唯一键的字典。因此,每次您尝试使用

    保存新 ID

    ID[行[0]] = 行[1]

    如果该字典键相同,它将覆盖前一个。例如:1000.jpg

    相反,我建议使用元组列表并在执行过程中添加列表

    >>> with open('old-names-new-names.csv') as csvfile:
    ...     timeReader = csv.reader(csvfile, delimiter = ',')
    ...     for row in timeReader:
    ...             IDs.append((row[0], row[1]))
    ... 
    >>> IDs
    [('1000.jpg', '1000-10x10.jpg'), ('1000.jpg', '1000-12x12.jpg'), ('1000.jpg', '1000-15x15.jpg')]
    

    然后您可以遍历 ID 来执行重命名逻辑并在元组上使用索引:

    >>> for ID in IDs:
    ...     print('old name:' + ID[0] + ' new name:' + ID[1])
    ... 
    old name:1000.jpg new name:1000-10x10.jpg
    old name:1000.jpg new name:1000-12x12.jpg
    old name:1000.jpg new name:1000-15x15.jpg
    

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多