![](/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.