读取子文件夹python

拉斐尔·阿迪塔玛

我正在尝试制作一个数据解析器,但在我的项目中使用了 python,但每个文件都位于彼此不同的文件夹中。目前我能够读取第一个文件夹,但我还没有弄清楚如何读取该文件夹并将其放入 for 循环

import os 

r_path='//esw-fs01/esw_niagara_no_bck/BuildResults/master/0.1.52.68_390534/installation_area/autotestlogs_top/'
sd_path='/.'

root = os.listdir(r_path)
subdir=os.listdir(sd_path)
for entry in root:
    # print(entry)
    if os.path.isdir(os.path.join(r_path, entry)):
        for subentry in subdir:
            if os.path.isdir(os.path.join(r_path,'/ConfigurationsTest_19469')):
                print(subentry)

对于第二个 for 循环,我想迭代autotestlogs文件夹中的每个文件夹。我试图做到,但它显然不起作用。请帮忙谢谢

克罗诺斯

我觉得你的订单有点乱。如果您subdir = os.listdir(sd_path)在循环之前执行此操作,则可能无法获取子目录,因为您需要使用父目录来获取它们。

因此,在您检查“条目”是一个文件夹之后的循环中,您可以将此文件夹的绝对路径存储在一个变量中,然后使用 os.listdir() 列出它的内容。然后你可以遍历这些并解析它们。

我会怎么做:

import os

r_path='//esw-fs01/esw_niagara_no_bck/BuildResults/master/0.1.52.68_390534/installation_area/autotestlogs_top/'

root = os.listdir(r_path)

for entry in root:
    # print(entry)
    subdir_path = os.path.join(r_path, entry) #  create the absolute path of the subdir
    if os.path.isdir(subdir_path):  # check if it is a folder
        subdir_entries = os.listdir(subdir_path)  # get the content of the subdir
        for subentry in subdir_entries:
            subentry_path = os.path.join(subdir_path, subentry)  # absolute path of the subentry
            # here you can check everything you want for example if the subentry has a specific name etc
            print(subentry_path)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python/os.listdir/读取子文件夹

Python exchangelib在子文件夹中读取邮件

使用Python搜索(在文件夹和子文件夹中)并将文件读取到数据框列表中

Python递归文件夹读取

读取子文件夹中的csv文件

使用尾部递归检索文件夹和子文件夹以用Java读取文件

从应用程序安装文件夹的子文件夹中读取文件

读取文件夹和子文件夹中的所有文件-进度和大小

如何读取Bash Shell中多个文件夹和子文件夹中的文件

在子文件夹中导入Python

Intellij 无法读取资源子文件夹中的图像

使用PyDrive(Python)访问文件夹,子文件夹和子文件

从与Python文件夹按顺序读取多个Excel文件

Python:从文件夹中读取多个json文件

从 Python 中的多个文件夹读取多个 CSV 文件?

从Python文件夹中读取HTML文件

从文件夹Python导入和读取所有文件

从文件夹 PYTHON 读取最新的 excel 文件

Python子文件夹模块无法导入其他子文件夹模块

PHP /从文件夹读取

搜索文件夹和子文件夹python中的多个文件

使用python删除文件夹和子文件夹中的pdf文件?

python:通过子文件夹名称重命名子文件夹中的文件

在 Python 中浏览和合并文件夹及其子文件夹中的 Excel 文件

遍历文件夹并为每个文件夹创建新的子文件夹,然后在 Python 中移动图像

在Python中浏览文件和子文件夹

无法在同级子文件夹之间导入python文件

Python在子文件夹中导入csv文件

创建一个循环以打开文件夹中的子文件夹,读取json文件并输出为csv