0

我有一个 python 脚本,它在一开始就从输入数据文件中读取这一行:

    x,y = genfromtxt('data1.txt').T

然后我继续对 x,y 进行处理(它取决于一个固定参数,例如 n=5)。最后我用这一行生成输出文件

    with open('output_data1_n{0}.txt'.format(num),'wb') as file: 

这给了我 output_data1_n5.txt 并在上面写了 xnew 和 ynew 。

问题:我有一个包含许多 txt 文件的目录!如何系统地为该目录中的所有文件执行此工作,而不是为每个输入文件手动运行?

应该是这样的:获取txt文件(例如使用os.walk?)作为字符串并将其替换为输入,然后生成包含参数n的输出名称。

我很欣赏你的建议。

4

2 回答 2

1

试试这个glob模块

它使您可以使用一些通配符获取目录中的文件名列表。

例子:

from glob import glob
from os import path

def get_files_in(folder, pattern='*.txt'):
    return glob(path.join(folder, pattern))

用法:

get_files_in('C:/temp') # files in C:/temp that are ending with .txt
get_files_in('C:/temp', '*.xml') # files in C:/temp that are ending with .xml
get_files_in('C:/temp', 'test_*.csv') # files in C:/temp that start with test_ and end in .csv
于 2013-05-30T09:29:02.300 回答
1

正如 Inbar Rose 已经解释的那样,您可以使用glob. 要将输入文件名转换为适当的输出文件名,您可以使用正则表达式从输入名称中提取文件编号,然后使用它来构造输出名称。

像这样的东西:

import os
import glob
import re

inputPath = '.' # the directory where your files are stored
num = 5         # the fixed parameter, n

# first obtain all the data*.txt files in the directory
for inputName in glob.glob(os.path.join(inputPath,'data*.txt')):

  # attempt to extract the file number from the input name
  fileNum = re.findall(r'data([0-9]+)\.txt',inputName)
  # if not successful, skip this file
  if not fileNum: continue

  # create the output filename using the fle number and the fixed parameter
  outputName = 'output_data{0}_{1}.txt'.format(fileNum[0],num)
  # add the input path to the filename, or use a different path if necessary
  outputName = os.path.join(inputPath,outputName)

  # process the file
  x,y = genfromtxt(inputName).T
  with open(outputName,'wb') as file: 
    # do the rest of your code here
    pass
于 2013-05-30T12:12:23.120 回答