I highly recommend this path module, written by Jason Orendorf:
http://pypi.python.org/pypi/path.py/2.2
Unfortunately, his website is currently down, but you can download it from the above link (or via easy_install if you want).
Using this route module, you can perform various actions along the paths, including the required walk files. Here is an example:
from path import path my_path = path('.') for file in my_path.walkfiles(): print file for file in my_path.walkfiles('*.pdf'): print file
There are also convenient functions for many other things related to paths:
In [1]: from path import path In [2]: my_dir = path('my_dir') In [3]: my_file = path('readme.txt') In [5]: print my_dir / my_file my_dir/readme.txt In [6]: joined_path = my_dir / my_file In [7]: print joined_path my_dir/readme.txt In [8]: print joined_path.parent my_dir In [9]: print joined_path.name readme.txt In [10]: print joined_path.namebase readme In [11]: print joined_path.ext .txt In [12]: joined_path.copy('some_output_path.txt') In [13]: print path('some_output_path.txt').isfile() True In [14]: print path('some_output_path.txt').isdir() False
There are other operations that can be performed, but these are some of the ones that I use most often. Note that the path class inherits from string , so it can be used wherever string used. Also note that two or more path objects can be easily combined using the overridden / operator.
Hope this helps!
source share