![](/img/trans.png)
[英]How can I use Python to walk through files in directories and output a pandas data frame given certain constraints?
[英]I want to use python to walk through directories to get to text files and processed them
我在目录中有很多文件夹:
/home/me/Documents/coverage
/coverage
包含50个以H
开头的文件夹:
/home/me/Documents/coverage/H1 (etc)
每个H***
文件夹中都有一个文本文件,我需要从中提取数据。
我一直在尝试使用glob和os.walk
来使用保存在/coverage
的脚本进入每个H
文件夹,打开.txt
文件并进行处理,但是我一点都不走运。
这将是一个很好的起点吗? (其中path
= /coverage
)
for filename in glob.glob(os.path.join(path, "H*")):
folder = open(glob.glob(H*))
然后尝试打开.txt
文件吗?
只需使用全局通配符将所有txt文件收集在一起即可。 你可以那样做。
import glob
path = "/home/me/Documents/coverage/H*/*.txt"
for filename in glob.glob(path):
fileStream = open(filename )
干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.