python 递归提取或者访问子文件夹所有指定文件

import os

dir_txt = "data/demo/"

txt_path_List = []
cnt_2 = 0
g = os.walk(dir_txt)
for path, d, filelist in g:
    for filename in filelist:
        if filename.endswith('.txt'):
            if "hello" in filename:
                continue
            print("cnt=", cnt_2, "filename=", filename)
            cnt_2 += 1
            path_txt = os.path.join(path, filename)
            txt_path_List.append(path_txt)
print("len =", len(txt_path_List))
print("example 0=",txt_path_List[0] )

python path的一些:

import os

root_file = "/media/algo/data_1/everyday/20230203/snowy"
basename_ = os.path.basename(root_file)
dirname_ = os.path.dirname(root_file)
print("basename_=", basename_)
print("dirname_=", dirname_)
#打印如下
basename_= snowy
dirname_= /media/algo/data_1/everyday/20230203
posted @ 2023-02-02 16:06  无左无右  阅读(81)  评论(0编辑  收藏  举报