【发布时间】:2015-11-24 03:39:31
【问题描述】:
我有两个文件的 50 个实例,它们位于一个目录的 50 个单独文件夹中。我正在尝试从每个文件夹中的两个文件中读取和提取信息,并将两个文件中的信息同时附加到一个列表中,同时在包含它们的文件夹中。 (因此它们将通过附加到相同的列表索引来关联)我正在使用 os.walk 并在文件被识别后立即打开文件。 (或试图)。当我运行时,似乎有问题的文件永远不会被打开,而且绝对没有任何东西被附加到我的列表中。有人可以告诉我我在这里所拥有的是否完全荒谬,因为这对我来说似乎是合乎逻辑的,但它不起作用。
import os
import sys
#import itertools
def get_theList():
#specify directory where jobs are located
#can also set 'os.curdir' to rootDir to read from current
rootDir = '/home/my.user.name/O1/injections/test'
这里没有问题;这是正确的
B_sig = []
B_gl = []
SNR_net = []
a = 0
for root, dirs, files in os.walk(rootDir):
for folder in dirs:
for file in folder:
if file == 'evidence_stacked.dat':
print 'open'
a+=1
ev_file = open(file,"r")
ev_lin = ev_file.split()
B_gl.append(ev_lin[1])
B_sig.append(ev_lin[2])
print ev_lin[1]
ev_file.close()
if file == 'snr.txt':
net_file = open(file,"r")
net_lines=net_file.readlines()
SNR_net.append(net_lines[2])
net_file.close()
print 'len a'
print a
输出为 0
print 'B_sig'
print B_sig
print len(B_sig)
print 'B_net'
print B_gl
print len(B_gl)
print 'SNR_net'
print SNR_net
print len(SNR_net)
if __name__ == "__main__":
get_theList()
【问题讨论】:
-
您指的是
'rootDir',而您的意思可能是rootDir。前者将寻找一个命名“rootDir”的文件夹,而不是你创建的rootDir变量。 -
谢谢,这是一个问题。
标签: python list file readfile os.walk