Skip to content Skip to sidebar Skip to footer

How To Get All The Files From Multiple Folders With The Same Names

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

Solution 1:

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']),
]

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

Post a Comment for "How To Get All The Files From Multiple Folders With The Same Names"