Buscar ficheros en el ordenador

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


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.


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.

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

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

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

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.