Walk a directory/Recursively: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: First example changed to work on Python 3.x too)
Line 1,264: Line 1,264:


=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|3.x}}
{{works with|Python|2.3+}}
{{works with|Python|2.3+}}


This uses the standard ''os.walk()'' "[[generator]]".
This uses the standard ''os.walk()'' "[[generator]]".


<lang python>import fnmatch
<lang python>import os
import os


rootPath = '/'
rootPath = '/'
extension = '.mp3'
pattern = '*.mp3' # Can include any UNIX shell-style wildcards
for root, dirs, files in os.walk(rootPath):
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
for filename in files:
print os.path.join(root, filename)</lang>
if filename.endswith(extension):
print( os.path.join(root, filename))</lang>


{{works with|Python|<nowiki>2.x</nowiki>}}
{{works with|Python|<nowiki>2.x</nowiki>}}