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

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

问题描述:

驱动器上有许多具有相同名称的文件夹(不要问它是如何发生的).我想从具有相同名称的文件夹中获取所有文件的名称.主要的挑战是我不知道这些文件夹的所有位置.

There are many folders with the same name on the drive (don't ask how it happened). I want to get the names of all the files from the folders with the same name. The main challenge is the I don't know all the locations of these folders.

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

以下是一种可能的方法:

Here's one possible method:

import os


hits = []

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

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

... this will create a list of directory paths and their contents, like so (this is a partial result from running against / for paths ending in /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少得多,所以您可能会得到更多有用的结果.

Since I imagine the directory name you're searching for is considerably less common than /bin, you'll probably have more useful results.