问题
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/
I try to do smth like this:
for name in glob.glob('*/folder_needed/*'):
print name
回答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.
来源:https://stackoverflow.com/questions/54011734/how-to-get-all-the-files-from-multiple-folders-with-the-same-names