简体   繁体   中英

Python3 Rename files in a directory importing the new names from a txt file

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM