How to load some specific elements in a folder in python
I have a folder like this:
/foo
1.t开发者_如何转开发xt
2.txt
2.jpg
3.txt
4.txt
5.txt
5.jpg
There are images that corresponds to some of the .txt files. What I want to do is to load all txt files in this folder including its file name into a python 2D array. How can I achieve that?
import re
import hjson
import os
for subdir, dirs, files in os.walk("/foo"):
for file in files:
fileName=os.path.join(subdir, file)
if(fileName.endswith(".txt")):
f1 = open(fileName, "r", encoding='utf-8')
code = f1.read()
#Here You can find all the .txt files in /foo directory
精彩评论