如何使用正则表达式找到数字,填充零并在Python中替换路径?

时间:2015-03-15 16:35:38

标签: python regex replace os.path

我正在尝试获取目录中所有.txt文件的文件路径,并替换每个文件的根目录,并使用不同的填充长度填充文件路径的零。考虑文件列表的示例:

./Old directory/ABC 01/XYZ 1 - M 1.txt
./Old directory/ABC 01/XYZ 1 - M 2.txt
./Old directory/ABC 01/XYZ 1 - M 3.txt

现在要求Python代码给我这个输出:

./New directory/ABC 00001/XYZ 0001 - M 001.txt
./New directory/ABC 00001/XYZ 0001 - M 002.txt
./New directory/ABC 00001/XYZ 0001 - M 003.txt

可重现的代码(我的努力):

import os
import re
files = []
for root, directories, files in os.walk('./Old directory'):
    files = sorted([f for f in files if os.path.splitext(f)[1] in ('.txt')])
    for file in files:
        files.append(os.path.join(root, file))
for file in files:
    file.replace('./Old directory', './New directory')

2 个答案:

答案 0 :(得分:0)

我怀疑这很容易,但看起来你很亲密。

import re
...
for file in files:
    file = file.replace('./Old directory', './New directory')
    p = re.compile(ur'(\d+)')
    file = re.sub(p, u"000$1", file)

View testing example

答案 1 :(得分:0)

在代码中将两个不同的目的使用相同的变量files是致命的 - 我将一个实例更改为filenames,并补充了代码以进行零填充。

import os
import re
filenames = []
for root, directories, files in os.walk('./Old directory'):
    files = sorted([f for f in files if os.path.splitext(f)[1] in ('.txt')])
    for file in files:
        filenames.append(os.path.join(root, file))
def padzeros(s, m, g, width):   # pad the group g of match m in string s 
    return s[:m.start(g)]+m.group(g).zfill(width)+s[m.end(g):]
for file in filenames:
    file = file.replace('./Old directory', './New directory')
    m = re.search(r'\D+(\d+)\D+(\d+)\D+(\d+)', file)
    # important: pad from last to first match
    file = padzeros(file, m, 3, 3)
    file = padzeros(file, m, 2, 4)
    file = padzeros(file, m, 1, 5)
    print file