简体   繁体   English

如何打印文件夹/子文件夹并从中导入文件?

[英]How can I print folder/sub-folders and import files from them?

How can I print folder/sub-folder names and import files from each folder?如何打印文件夹/子文件夹名称并从每个文件夹导入文件?

1.folder 1.1. 1.文件夹 1.1. folder 1.1.1.folder 1.1.1.1.folder 1.1.1.2.folder 1.1.2.folder 1.1.3.folder 1.2.folder 1.2.1.folder 1.2.2.folder文件夹 1.1.1.文件夹 1.1.1.1.文件夹 1.1.1.2.文件夹 1.1.2.文件夹 1.1.3.文件夹 1.2.文件夹 1.2.1.文件夹 1.2.2.文件夹

You can use os.listdir() to get all files/folders in a directory and then loop over each and do the same recursively.您可以使用 os.listdir() 获取目录中的所有文件/文件夹,然后遍历每个文件/文件夹并递归执行相同操作。 Something like:就像是:

import os
from queue import Queue

files = []

q = Queue()
q.put(PATH)

while not q.empty():
    path = q.get()
    
    for file in os.listdir(path):
        if os.path.isdir(file):
            print(file)
            q.put(file)
        else:
            files.append(file)

This would extract all files in each subdirectory to the files array and in the loop print each searched directory.这会将每个子目录中的所有文件提取到files数组中,并在循环中打印每个搜索到的目录。 Make sure to change PATH to your starting directory for the search.确保将PATH更改为搜索的起始目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何将多个子文件夹中的图片复制到一个普通文件夹中并重命名? - How to copy images from multiple sub-folders to a common folder and rename them? 如何将特定文件从子文件夹复制到python中的新文件夹? - How to copy specific files from the sub-folders to a new folder in python? 如何从文件夹和子文件夹中保存音频文件 (.wav) 的频谱图? - How can i save spectrogram of audio file (.wav) from folders and sub-folders? 如何将特定文件与子文件夹分开? - How to separate specific files from sub-folders? Python 导入子文件夹 - Python import on sub-folders 如何在包含许多子文件夹和文件的python文件夹中检索某些文件 - How to retrieve certain files inside a folder containing many sub-folders and files python 将批处理图像文件从子文件夹复制到父文件夹 - Copying batch image files from sub-folders to parent folders 如何在不指定子文件夹名称的情况下自动合并来自多个子文件夹的多个 CSV? - How to automatically merge multiple CSVs from multiple sub-folders, without specifying sub-folder name? 如何编译位于带有子文件夹的文件夹中的 Python 软件 - How to compile my Python software that's in a folder with sub-folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM