简体   繁体   中英

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)

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)

You should probably try using regular expressions, like

import re

<...>

newfilename = re.sub(r'-\d{8}-\d{6}\b', '', oldfilename)

<...>

This will replace any 'hyphen, 8 digits, hyphen, 6 digits' not followed by letter, digit or underscore with empty string in your filename. Hope I got you right.

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