Buscar ficheros en el ordenador


/ Published in: Python
Save to your folder(s)

Overview

In an earlier post "OS.walk in Python", I described how to use os.walk and showed some examples on how to use it in scripts.

In this article, I will show how to use the os.walk() module function to walk a
directory tree, and the fnmatch module for matching file names.

What is OS.walk?

It generates the file names in a directory tree by walking the tree either
top-down or bottom-up.

For each directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).

dirpath # is a string, the path to the directory.

dirnames # is a list of the names of the subdirectories in dirpath
(excluding '.' and '..').

filenames # is a list of the names of the non-directory files in dirpath.

Note that the names in the lists contain no path components.

To get a full path (which begins with top) to a file or directory in dirpath,
do os.path.join(dirpath, name).

For more information, please see the Python Docs.

What is Fnmatch

The fnmatch module compares file names against glob-style patterns such as used
by Unix shells.

These are not the same as the more sophisticated regular expression rules.

It's purely a string matching operation.

If you find it more convenient to use a different pattern style, for example
regular expressions, then simply use regex operations to match your filenames.

http://www.doughellmann.com/PyMOTW/fnmatch/

What does it do?

The fnmatch module is used for the wild-card pattern matching.

Simple Matching
fnmatch() compares a single file name against a pattern and returns a boolean
indicating whether or not they match.

The comparison is case-sensitive when the operating system uses a case-sensitive
file system.

Filtering
To test a sequence of filenames, you can use filter().

It returns a list of the names that match the pattern argument.


Copy this code and paste it in your HTML
  1. import fnmatch
  2. import os
  3.  
  4. ############################################
  5. # Find all mp3 files #
  6. # #
  7. # This script will search for *.mp3 #
  8. # files from the rootPath ("/") #
  9. # #
  10. ############################################
  11.  
  12. rootPath = '/'
  13. pattern = '*.mp3'
  14.  
  15. for root, dirs, files in os.walk(rootPath):
  16. for filename in fnmatch.filter(files, pattern):
  17. print( os.path.join(root, filename))
  18.  
  19.  
  20. ############################################
  21. # Search computer for specific files #
  22. # #
  23. # This script uses 'os.walk' and #
  24. # 'fnmatch' with filters to search #
  25. # the hard-drive for all image files #
  26. # #
  27. ############################################
  28.  
  29. images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff']
  30. matches = []
  31.  
  32. for root, dirnames, filenames in os.walk("C:\\"):
  33. for extensions in images:
  34. for filename in fnmatch.filter(filenames, extensions):
  35. matches.append(os.path.join(root, filename))

URL: http://www.pythonforbeginners.com/systems-programming/os-walk-and-fnmatch-in-python/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.