Search code examples
pythonbatch-rename

How to remove characters from multiple files in python


I'm, trying to write a simple program to batch rename files in a folder.

file format:

11170_tcd001-20160824-094716.txt

11170_tcd001-20160824-094716.rst

11170_tcd001-20160824-094716.raw

I have 48 of the above with a different 14 digit character configuration after the first "-".

My final goal is to convert the above to:

11170_tcd001.txt

11170_tcd001.rst

11170_tcd001.raw

I know it's possible to os.rename files in python. However, I can't figure out how to batch rename multiple files with a different character configuration.

Is this possible?

some pseudocode below of what I would like to achieve.

import os

pathiter = (os.path.join(root, filename)
    for root, _, filenames in os.walk(folder)
    for filename in filenames
)
for path in pathiter:
    newname =  path.replace('14 digits.txt', ' 0 digits.txt')
    if newname != path:
        os.rename(path,newname)

Solution

  • If you are looking for a non-regex approach and considering your files all match that particular pattern you are expecting, what you can do first is get the extension of the file using splitext:

    from os.path import splitext
    file_name = '11170_tcd001-20160824-094716.txt'
    extension = splitext(file_name)[1]
    print(extension) # outputs: .txt
    

    Then, with the extension in hand, split the file_name on the - and get the first item since you know that is the part that you want to keep:

    new_filename = file_name.split('-')[0]
    print(new_filename) # 11170_tcd001
    

    Now, append the extension:

    new_filename = new_filename + extension
    print(new_filename) # 11170_tcd001.txt
    

    Now you can proceed with the rename:

    os.rename(file_name, new_filename)