Python rename files reading names from csv file

后端 未结 3 1523
醉话见心
醉话见心 2021-01-07 06:28

Hi there i\'ve been trying to adapt this to my needs but I\'m just a newbe in python, I have a csv file with multiple columns and rows, important columns are 1 = old name of

相关标签:
3条回答
  • 2021-01-07 06:35

    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
    
    0 讨论(0)
  • 2021-01-07 06:44

    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
    
    0 讨论(0)
  • 2021-01-07 07:00

    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] + '!'
    
    0 讨论(0)
提交回复
热议问题