【发布时间】:2019-05-22 22:48:09
【问题描述】:
我编写了一个函数来从我的原始数据文件中去除双空格:
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 中编码 filename 和 folder?
【问题讨论】:
标签: python file directory filenames