正在处理 Python 中的文件名

Processing filenames in Python

我编写了一个函数来从我的原始数据文件中去除双空格:

def fixDat(file):
    '''
    Removes extra spaces in the data files. Replaces original file with new
    and renames original to "...._original.dat".
    '''
    import os

    import re
    with open(file+'.dat', 'r') as infile:
        with open(file+'_fixed.dat', 'w') as outfile:
            lines = infile.readlines()
            for line in lines:
                fixed = re.sub("\s\s+" , " ", line)
                outfile.write(fixed)

    os.rename(file+'.dat', file+'_original.dat')
    os.rename(file+'_fixed.dat', file+'.dat')

我需要使用此函数处理的文件夹中有 19 个文件,但我不确定如何解析文件名并将它们传递给函数。像

for filename in folder:
    fixDat(filename)

但是如何在 Python 中编码 filenamefolder

如果我没理解错的话,你问的是the os module's .walk() functionality。示例如下:

import os
for root, dirs, files in os.walk(".", topdown=False): # "." uses current folder
    # change it to a pathway if you want to process files not where your script is located
    for name in files:
        print(os.path.join(root, name))

带有文件名输出,可以提供给您的 fixDat() 函数,例如:

./tmp/test.py
./amrood.tar.gz
./httpd.conf
./www.tar.gz
./mysql.tar.gz
./test.py

请注意,这些都是字符串,因此您可以将脚本更改为:

import os
for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        if name.endswith('.dat'): # or some other extension
            print(os.path.join(root, name))
            fixDat(os.path.join(root, name))