开发者

How to load some specific elements in a folder in python

开发者 https://www.devze.com 2022-12-07 22:06 出处:网络
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 t

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 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号