2

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!

madhead
  • 31,729
  • 16
  • 153
  • 201
MRM
  • 23
  • 3
  • Be more precise with your example and format your question to make it clear for "visual" people, please :) – J. Chomel Apr 23 '16 at 08:48

2 Answers2

2

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

Community
  • 1
  • 1
Dhineshkumar
  • 109
  • 10
1

The following code will do the job for your specific use-case, though can make it more general purpose re-namer.

import os # os is a library that gives us the ability to make OS changes

def file_renamer(list_of_files, new_file_name_list):
    for file_name in list_of_files:
        for (new_filename, barcode_infile) in new_file_name_list:
            # as per the mentioned filename pattern -> xxxx.1.xxxx.[barcode]
            barcode_current = file_name[12:19] # extracting the barcode from current filename
            if barcode_current == barcode_infile:
                os.rename(file_name, new_filename)  # renaming step
                print 'Successfully renamed %s to %s ' % (file_name, new_filename)


if __name__ == "__main__":
    path = os.getcwd()  # preassuming that you'll be executing the script while in the files directory
    file_dir = os.path.abspath(path)
    newname_file = raw_input('enter file with new names - or the complete path: ')
    path_newname_file = os.path.join(file_dir, newname_file)
    new_file_name_list = []
    with open(path_newname_file) as file:
        for line in file:
            x = line.strip().split(',')
            new_file_name_list.append(x)

    list_of_files = os.listdir(file_dir)
    file_renamer(list_of_files, new_file_name_list)

Pre-assumptions: newnames.txt - comma

0000.1.0000.1234567,1234567
0000.1.0000.1234568,1234568
0000.1.0000.1234569,1234569
0000.1.0000.1234570,1234570
0000.1.0000.1234571,1234571

Files

1111.1.0000.1234567
1111.1.0000.1234568
1111.1.0000.1234569 

were renamed to

0000.1.0000.1234567
0000.1.0000.1234568
0000.1.0000.1234569

The terminal output:

>python file_renamer.py
enter file with new names: newnames.txt
The list of files -  ['.git', '.idea', '1111.1.0000.1234567', '1111.1.0000.1234568', '1111.1.0000.1234569', 'file_renamer.py', 'newnames.txt.txt']
Successfully renamed 1111.1.0000.1234567 to 0000.1.0000.1234567
Successfully renamed 1111.1.0000.1234568 to 0000.1.0000.1234568
Successfully renamed 1111.1.0000.1234569 to 0000.1.0000.1234569
Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
  • If I have pre-assumed anything as not what you meant, leave it in the comment section and I'll update the code. – Nabeel Ahmed Apr 23 '16 at 16:38
  • Thank you so much for your help. I understood the first part but not quite the "if" part, maybe because I did not express myself very well. Plus I don't have to worry about the barcode anymore. – MRM Apr 24 '16 at 12:38
  • In a directory I have several files and .txt file tab delimited. In the txt file, the names in the second column will match the filename of my files in my directory.In the first column of the txt I have the names I want to rename my files for. I think I should list my files in my directory and if the name match the second column in my txfile rename to the name in the same line from the first column. does this make sense ? Thank you a lot! – MRM Apr 24 '16 at 12:38
  • My bad, the lines in the if will only be executed when module (the .py file) is run as a script i.e. $ python module.py - http://stackoverflow.com/questions/419163/what-does-if-name-main-do/15789709#15789709 – Nabeel Ahmed Apr 24 '16 at 12:45
  • Good to know. If it qualifies as a solution, you should mark it as the answer - 'd be helpful for future SO visitors. – Nabeel Ahmed Apr 25 '16 at 05:43