我想從根目錄的子目錄中的文件訪問和處理信息。我曾嘗試使用os.walk,它使我能夠訪問這些文件,但是如何訪問它們的內容?我想在這些子目錄中具有相同名稱的特定文件,但這些子目錄中還有其他文件。這是我曾嘗試過的:如何使用os.walk從子目錄中的文件訪問信息?
import os
import numpy as np
for root, dirs, files in os.walk("/rootDir/"):
for file in files:
if file.endswith(('sum.txt')):
print file #Here, the desired file name is printed
PIs = []
for line in file:
print line #Here, I only get 's' printed, which I believe is the first letter in 'sum.txt'
line = line.rstrip()
line = line.split('\t')
PIs.append(line[2])
print PIs #nothing is collected so nothing is printed
如何循環遍歷根目錄中這些子目錄中所需文件的行?
額外的問題:
我的回答得到了我的第一個問題,我現在有另一個。在根目錄下的目錄中有許多子目錄。我想只從一個在所有目錄中具有相同名稱的子目錄訪問信息。這是我的嘗試:
for root, dirs, files in os.walk("/rootPath/"):
for dname in dirs:
#print dname, type(dname)
allPIs = []
allDirs = []
if dname.endswith('code_output'): #I only want to access information from one file in sub-directories with this name
ofh = open("sumPIs.txt", 'w')
ofh.write("path\tPIs_mean\n")
for fname in files: #Here i want to be in the code_output sub-directory
print fname #here I only want to see files in the sub-directory with the 'code_output' end of a name, but I get all files in the directory AND sub-directory
if fname.endswith('sumAll.txt'):
PIs = []
with open(os.path.join(root,fname), 'r') as fh_in:
for line in fh_in:
line = line.rstrip()
line = line.split('\t')
PIs.append(int(line[2]))
PIs_mean = numpy.mean(PIs)
allPIs.append(PIs_mean)
allDirs.append(filePath)
爲什麼期末「code_output」這個循環在目錄中的所有文件,並不僅是子目錄的名稱?
我把'PIs = []'放在for循環中:P –
@RicardoCárdenes確實 –
你需要os.path.join(root,fname)。 fname只是文件的基本名稱。 – tdelaney