Python rename files reading names from csv file

耗尽温柔 提交于 2019-12-01 11:48:49

This will rename each matching file, and report any errors trying to rename. It will not attempt to move non-existent files.

import os, unicodecsv as csv
# open and store the csv file
IDs = {}
with open('documentos_corpus_ladino.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 = 'txt_orig/'
tmpPath = 'txt_tmp/'
for oldname in os.listdir(path):
    # ignore files in path which aren't in the csv file
    if oldname in IDs:
        try:
            os.rename(os.path.join(path, oldname), os.path.join(tmpPath, IDs[oldname]))
        except:
            print 'File ' + oldname + ' could not be renamed to ' + IDs[oldname] + '!'

You should make use of the dictionary IDs that you created from your CSV:

for filename in os.listdir(path):
    oldname = filename
    newname = IDs[oldname]
    os.rename(path + filename, tmpPath + newname)

But you probably should use some kind of error checking.. (Edit As the other answers have pointed out it's best to use also os.path.join) Maybe something along these lines:

failed = []
for oldname in os.listdir(path):
    try:
        old = os.path.join(path, oldname)
        new = os.path.join(tmpPath, IDs[oldname])
        os.rename(old, new)
    except KeyError, OSError:
        failed.append(oldname)

print failed

You're iterating on the file and store old and new names in IDs but don't use it and just try to read further from the file (which will fail obviously since you've already read the whole file by that time). IOW you should use your IDs dict to get new names (using the oldname as key) instead, ie:

path = 'txt_orig' # no trailing slash required
tmpPath = 'txt_tmp' # idem
for filename in os.listdir(path):
    try:
       newname = IDs[filename]
    except KeyError:
       print "no new name for '%s'" % filename
       continue
    else:     
        os.rename(os.path.join(path, filename), os.path.join(tmpPath, newname))

Now there's a much simpler solution: just rename the files as you iterate on the csv file:

path = 'txt_orig'
tmp_path = 'txt_tmp'

with open('documentos_corpus_ladino.csv','rb') as csvfile:
    reader = csv.reader(csvfile, delimiter = ',')
    for row in reader:
       oldname = os.path.join(path, row[0])
       if os.path.exists(oldname):
           newname = os.path.join(tmp_path, row[1])
           os.rename(oldname, newname)
           print >> sys.stderr, "renamed '%s' to '%s'" % (oldname, newname)
       else:
           print >> sys.stderr, "file '%s' not found" % oldname
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!