我编写了一个函数,从原始数据文件中去掉两个空格:
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
如果我理解正确,你是在问the ^{} module's ^{} functionality 。例如:
文件名输出可以提供给
fixDat()
函数,例如:请注意,这些都是字符串,因此您可以将脚本更改为:
相关问题 更多 >
编程相关推荐