如何从多个具有相同名称的文件夹中获取所有文件

时间:2019-01-02 18:58:37

标签: python-3.x glob

驱动器上有许多同名的文件夹(不要问它是怎么回事)。我想从具有相同名称的文件夹中获取所有文件的名称。主要的挑战是我不知道这些文件夹的所有位置。

D:
folder1/abc/folder_needed/
folder2/qwe/qwe2/folder_needed/
...
folder_zxc/x/y/folder_needed/

我尝试这样做:

for name in glob.glob('*/folder_needed/*'):
    print name

1 个答案:

答案 0 :(得分:0)

这是一种可能的方法:

import os


hits = []

for dirpath, dirnames, filenames in os.walk('/path/to/your_drive'):
    if dirpath.endswith('/folder_needed'):
        hits.append((dirpath, filenames))

...这将创建目录路径及其内容的列表,如下所示(这是针对以/结尾的路径针对/bin运行的部分结果):

[
    ('/home/roundel/projects/zold/bin', ['zold-nohup', 'zold']),
    ('/home/roundel/projects/monday/venv/bin', ['easy_install-3.4', 'flask', 'activate_this.py', 'python3', 'pip', 'gunicorn', 'python-config', 'pbr', 'pip3', 'pip3.4', 'gunicorn_paster', 'activate.csh', 'activate', 'raven', 'python3.4', 'python', 'wheel', 'activate.fish', 'easy_install']),
    ('/home/roundel/projects/budgeting_app/venv/bin', ['easy_install-3.4', 'flask', 'activate_this.py', 'python3', 'pip', 'gunicorn', 'python-config', 'pip3', 'pip3.4', 'gunicorn_paster', 'activate.csh', 'activate', 'raven', 'python3.4', 'python', 'wheel', 'activate.fish', 'easy_install']),
    ('/home/roundel/projects/bitcoin/hdwallets/venv/bin', ['pilfont.py', 'pilfile.py', 'tx', 'easy_install-3.4', 'ku', 'pilconvert.py', 'enhancer.py', 'player.py', 'activate_this.py', 'python3', 'pip', 'spend', 'python-config', 'pip3', 'pildriver.py', 'pip3.4', 'createfontdatachunk.py', 'viewer.py', 'pilprint.py', 'activate.csh', 'painter.py', 'activate', 'block', 'gifmaker.py', 'fetch_unspent', 'python3.4', 'cache_tx', 'python', 'bu', 'wheel', 'explode.py', 'activate.fish', 'easy_install', 'thresholder.py', 'qr', 'genwallet']),
    ('/home/roundel/projects/crypto_hivemind/venv/bin', ['tx', 'easy_install-3.4', 'ku', 'flask', 'activate_this.py', 'python3', 'pip', 'spend', 'python-config', 'pip3', 'pip3.4', 'activate.csh', 'activate', 'block', 'raven', 'fetch_unspent', 'python3.4', 'cache_tx', 'python', 'bu', 'wheel', 'activate.fish', 'easy_install', 'genwallet']),
    ('/home/roundel/Unity-2018.2.7f1/Editor/Data/MonoBleedingEdge/lib/mono/xbuild/14.0/bin', ['Microsoft.Build.Tasks.Core.dll', 'xbuild.exe', 'Microsoft.Build.dll', 'Microsoft.Build.Utilities.Core.dll', 'xbuild.pdb', 'Microsoft.Build.Engine.dll', 'Microsoft.CSharp.targets', 'Microsoft.Common.tasks', 'Microsoft.VisualBasic.targets', 'Mono.XBuild.Tasks.dll', 'xbuild.rsp', 'xbuild.exe.config', 'Microsoft.Build.Framework.dll', 'Microsoft.Build.xsd', 'Microsoft.Common.targets']),
    ('/home/roundel/Unity-2018.2.7f1/Editor/Data/MonoBleedingEdge/lib/mono/xbuild/12.0/bin', ['Microsoft.Build.Utilities.v12.0.dll', 'xbuild.exe', 'Microsoft.Build.Tasks.v12.0.dll', 'Microsoft.Build.dll', 'xbuild.pdb', 'Microsoft.Build.Engine.dll', 'Microsoft.CSharp.targets', 'Microsoft.Common.tasks', 'Microsoft.VisualBasic.targets', 'Mono.XBuild.Tasks.dll', 'xbuild.rsp', 'xbuild.exe.config', 'Microsoft.Build.Framework.dll', 'Microsoft.Build.xsd', 'Microsoft.Common.targets']),
    ('/home/roundel/Unity-2018.2.7f1/Editor/Data/MonoBleedingEdge/lib/mono/msbuild/15.0/bin', []),
    ('/home/roundel/Unity-2018.2.7f1/Editor/Data/MonoBleedingEdge/bin', ['cli', 'monobin-env', 'pedump', 'mono-env', 'mono']),
    ('/home/roundel/Unity-2018.2.7f1/Editor/Data/Tools/nodejs/bin', ['npm', 'node']),
]

由于我想您正在搜索的目录名称比/bin少得多,因此您可能会得到更有用的结果。