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
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