我有一个包含多个子目录的目录,每个子目录也包含多个目录。我有一个存在于所有子目录中的文件,需要根据子目录选择文件。我可以输入一些内容。根据目录筛选文件
like <cell 1> <cell 2> <cell 3>
each cell1
<job 1> <job 2> < job 3>
each job contain sample. txt
相似的cell2和cell 3.所以我想从每个cell/job1目录中提取sample.txt。 并编写下面的程序。 只是在修复问题后修改程序。我们可以做更多更好的办法
#!/usr/bin/py
import os
def find_all(name, path):
result = []
for root, dir, files in os.walk(path):
print "root %s dir %s" %(root, dir)
if "job1" in root:
print "\n"
if name in files:
result.append(os.path.join(root, name))
return result
name = "sample.txt"
path = "."
data = find_all(name, path)
print data
〜
进口操作系统;来自glob进口glob;对于glob中的f(“./cell */job */sample.txt”):if os.path.dirname(f).endswith(“job1”):whatever() – jlujan