Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中只读取目录的文件名,然后对所有目录执行相同的操作?_Python_String_Numpy_Replace_Readfile - Fatal编程技术网

如何在python中只读取目录的文件名,然后对所有目录执行相同的操作?

如何在python中只读取目录的文件名,然后对所有目录执行相同的操作?,python,string,numpy,replace,readfile,Python,String,Numpy,Replace,Readfile,我有一个python脚本,它在一开始就有一行代码,用于读取输入数据文件: x,y = genfromtxt('data1.txt').T 然后我继续对x,y进行处理(它取决于一个固定参数,例如n=5)。最后,我用这一行生成输出文件 with open('output_data1_n{0}.txt'.format(num),'wb') as file: 这将为我提供输出数据1和n5.txt,并在其上写入xnew和ynew 问题:我有一个包含许多txt文件的目录!如何系统地对该

我有一个python脚本,它在一开始就有一行代码,用于读取输入数据文件:

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

    with open('output_data1_n{0}.txt'.format(num),'wb') as file: 
这将为我提供输出数据1和n5.txt,并在其上写入xnew和ynew

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

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

谢谢你的建议。

试试这个

它可以让你在一个带有通配符的目录中获得一个文件名列表

例如:

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

正如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

你试过os.walk吗?我试过了,但我只得到了文件名,但无法将它们作为输入文件传递,然后生成输出名称!不用“data1.txt”,只需输入os.walk?的结果,谢谢您的详细回答。我确信它接近我想要的,只是给了我一个错误outputName='output_data{0}{1}.dat'。格式(fileNum[0],num)NameError:名称'fileNum'未定义我怀疑您在复制我的答案时可能键入了错误的内容,或者可能以破坏代码的方式更改了缩进。我刚刚将代码剪切并粘贴到一个文件中进行测试,除了需要注释掉
genfromtxt
行之外,它工作得非常好。我对测试也做了同样的操作。它不再给我那个错误,但也不会产生任何东西。脚本运行但没有输出。我想我没有得到输入名称,因为当我试图通过在outputName…行的前面放一个命令:“for I in range(0,1):print fileNum[I]”来检查名称时,它会给我错误信息。如何打印文件名以确保安全?我对python非常陌生,所以我会在for循环的第一行添加
print inputName
,以确保它正在查找文件。然后,
print outputaname
生成
outputName
后的行,查看它生成了什么。如果您想查看文件编号,
printfilenum[0]
-它是一个单项目数组。我假设您已经用一些有意义的东西填充了
inputPath
变量?您的输入文件的格式为
data1.txt
data2.txt
,等等。非常感谢您的参与!关键是我有3位数字和文件扩展名中的“K”,所以我必须将数据指定为[0-999]+K。现在一切正常。干杯