Python3 Rename files in a directory importing the new names from a txt file
Question: I have a directory containing multiple files. The name of the files follows
this pattern 4digits.1.4digits.[barcode] The barcode specifies each file and
it is composed by 7 leters. I have a txt file where in one column I have that
barcode and in the other column the real name of the file. What I would like
to do is to right a pyhthon script that automatically renames each file
according to the barcode to it s new name written in the txt file.

Is there anybody that could help me?

Thanks a lot!


Answer: I will give you the logic:

**1.** read the text file that contains barcode and
name.<http://www.pythonforbeginners.com/files/reading-and-writing-files-in-
python>.

for each line in txt file do as follows:

**2.** Assign the value in first(barcode) and second(name) column in two
separate variables say 'B' and 'N'.

**3.** Now we have to find the filename which has the barcode 'B' in it. the
link [Find a file in python](http://stackoverflow.com/questions/1724693/find-
a-file-in-python) will help you do that.(first answer, 3 rd example, for your
case the name you are going to find will be like '*B')

**4.** The previous step will give you the filename that has B as a part. Now
use the rename() function to rename the file to 'N'. this link will hep
you.<http://www.tutorialspoint.com/python/os_rename.htm>

Suggestion: Instead of having a txt file with two columns. You can have a csv
file, that would be easy to handle.

