简体   繁体   English

如何从python中的多个文件中删除字符

[英]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 "-". 我在上面的48个字符中,第一个“-”之后有一个不同的14位字符配置。

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. 我知道可以在python中使用os.rename文件。 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 : 如果您正在寻找一种非正则表达式的方法,并且考虑到所有文件都符合您期望的特定模式,那么您首先可以使用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: 然后,使用扩展名,在-上分割file_name并获得第一项,因为您知道这是您要保留的部分:

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. 这将用文件名中的空字符串替换所有不带字母,数字或下划线的“连字符,8位,连字符,6位”。 Hope I got you right. 希望我说对了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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