Jump to content

Walk a directory/Recursively: Difference between revisions

→‎{{header|Python}}: put back fnmatch,filter and add a link to its explanation
(→‎{{header|Python}}: First example changed to work on Python 3.x too)
(→‎{{header|Python}}: put back fnmatch,filter and add a link to its explanation)
Line 1,267:
{{works with|Python|2.3+}}
 
This uses the standard [http://docs.python.org/py3k/library/os.html?highlight=os.walk#os.walk os.walk()] module function to walk a directory tree, and the [http://docs.python.org/py3k/library/fnmatch.html fnmatch] module for matching file names.
This uses the standard ''os.walk()'' "[[generator]]".
 
<lang python>import osfnmatch
import os
 
rootPath = '/'
extensionpattern = '*.mp3'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
ifprint( filenameos.endswithpath.join(extensionroot, filename)):</lang>
print( os.path.join(root, filename))</lang>
 
{{works with|Python|<nowiki>2.x</nowiki>}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.