Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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_File Handling - Fatal编程技术网

如何从python中包含文件列表的单个文件打开多个文件,以及如何对它们进行处理?

如何从python中包含文件列表的单个文件打开多个文件,以及如何对它们进行处理?,python,file-handling,Python,File Handling,我有一个名为bar.txt的文件,它有如下文件列表 bar.txt- 1.txt 2.txt 3.txt bar.txt中的每个文件都有一些类似的内容 1.txt- spec=sadasdsad 2.txt- spec=dddddd 3.txt- spec=ppppppppppp outfile = open('bar.txt', "rw") outfile_1 = open('foo.txt', "w") for f in outfile:

我有一个名为bar.txt的文件,它有如下文件列表

bar.txt-

1.txt

2.txt

3.txt

bar.txt中的每个文件都有一些类似的内容

1.txt-

spec=sadasdsad

2.txt-

spec=dddddd

3.txt-

spec=ppppppppppp

 outfile = open('bar.txt', "rw")
 outfile_1 = open('foo.txt', "w")
     for f in outfile:
        f=f.rstrip()
        lines = open(f,'rw')
        lines = re.findall(".*SPEC.*\\n",lines)
        outfile_1.write(lines)
 outfile.close()
如何打开bar.txt中的所有文件,从所有文件中提取数据并存储在另一个名为foo.txt的文件中

在foo.txt中,我需要下面提到的提取数据

foo.txt-

spec=sadasdsad

spec=dddddd

spec=ppppppppppp

 outfile = open('bar.txt', "rw")
 outfile_1 = open('foo.txt', "w")
     for f in outfile:
        f=f.rstrip()
        lines = open(f,'rw')
        lines = re.findall(".*SPEC.*\\n",lines)
        outfile_1.write(lines)
 outfile.close()
我会这样做:

infle=open('bar.txt',“r”)
outfile=open('foo.txt',“w”)
line=infle.readline()
while line:
f=行.rstrip()
contents\u file=open(f,'rw')
contents=contents\u file.read()
outfile.write(内容)
f=infle.readline()
outfile.close()
我会这样做:

infle=open('bar.txt',“r”)
outfile=open('foo.txt',“w”)
line=infle.readline()
while line:
f=行.rstrip()
contents\u file=open(f,'rw')
contents=contents\u file.read()
outfile.write(内容)
f=infle.readline()
outfile.close()

您的代码几乎是正确的。我猜你几乎在所有事情上都使用了
f
变量,把事情搞砸了。因此,您将多个不同的内容分配给一个f变量。首先是outfile上的单行,然后是条带化的同一行,然后是另一个打开的文件,最后尝试在其作用域之外使用相同的f变量(for循环之外)。尝试对所有这些生物使用不同的变量

还要确保缩进正确(例如,示例中循环缩进的
是不正确的),并且regex
findall
不适用于字符串,而不是类似文件的对象,因此
findall
的第二个参数应该是
contentfile.read()

infle=open('bar.txt',“r”)
outfile=open('foo.txt',“w”)
对于填充中的f:
name=f.rstrip()
contentfile=open(名称为'rw')
#所有匹配项=re.findall(,contentfile.read())
结果=0#用你的所有匹配项做点什么
outfile.write(结果)
contentfile.close()
outfile.close()
infle.close()

您的代码几乎是正确的。我猜你几乎在所有事情上都使用了
f
变量,把事情搞砸了。因此,您将多个不同的内容分配给一个f变量。首先是outfile上的单行,然后是条带化的同一行,然后是另一个打开的文件,最后尝试在其作用域之外使用相同的f变量(for循环之外)。尝试对所有这些生物使用不同的变量

还要确保缩进正确(例如,示例中循环缩进的
是不正确的),并且regex
findall
不适用于字符串,而不是类似文件的对象,因此
findall
的第二个参数应该是
contentfile.read()

infle=open('bar.txt',“r”)
outfile=open('foo.txt',“w”)
对于填充中的f:
name=f.rstrip()
contentfile=open(名称为'rw')
#所有匹配项=re.findall(,contentfile.read())
结果=0#用你的所有匹配项做点什么
outfile.write(结果)
contentfile.close()
outfile.close()
infle.close()

为什么要使用python?bash会更快,因为实际上这个小spinet是我脚本的一部分,我的脚本是pythonFirst,尝试为不同的事情使用不同的变量。将多个不同的内容分配给一个
f
变量。首先是outfile上的单行,然后是条带化的同一行,然后是另一个打开的文件,最后尝试在其作用域之外使用相同的f变量(在
for
循环之外)。在bar.txt中,所有行都带有\n(新行字符),因为我得到的fie/dir不存在,所以我做了rstrip()如你所说,如果我尝试使用不同的变量,我会得到错误返回_compile(pattern,flags)?bash会更快,因为实际上这个小spinet是我脚本的一部分,我的脚本是pythonFirst,尝试为不同的事情使用不同的变量。将多个不同的内容分配给一个
f
变量。首先是outfile上的单行,然后是条带化的同一行,然后是另一个打开的文件,最后尝试在其作用域之外使用相同的f变量(在
for
循环之外)。在bar.txt中,所有行都带有\n(新行字符),因为我得到的fie/dir不存在,所以我做了rstrip()如你所说,如果我尝试使用不同的变量,我会得到错误返回_compile(pattern,flags)。findall(string)TypeError:预期的字符串或缓冲区在执行outfile.writelines(result)之后,而不是outfile.write(result)它正在工作..在执行outfile.writelines(result)之后(结果)它正在工作。如需帮助,我会试试这个。如需帮助,我会试试这个