Skip to content Skip to sidebar Skip to footer

Looking For File Traversal Functions In Python That Are Like Java's

In Java you can do File.listFiles() and receive all of the files in a directory. You can then easily recurse through directory trees. Is there an analogous way to do this in Pytho

Solution 1:

Yes, there is. The Python way is even better.

There are three possibilities:

1) Like File.listFiles():

Python has the function os.listdir(path). It works like the Java method.

2) pathname pattern expansion with glob:

The module glob contains functions to list files on the file system using Unix shell like pattern, e.g.

files = glob.glob('/usr/joe/*.gif')

3) File Traversal with walk:

Really nice is the os.walk function of Python.

The walk method returns a generation function that recursively list all directories and files below a given starting path.

An Example:

import os
from os.path import join
for root, dirs, files inos.walk('/usr'):
   print"Current directory", root
   print"Sub directories", dirs
   print"Files", files
You can even on the fly remove directories from "dirs" to avoid walking to that dir: if "joe" in dirs: dirs.remove("joe") to avoid walking into directories called "joe".

listdir and walk are documented here. glob is documented here.

Solution 2:

As a long-time Pythonista, I have to say the path/file manipulation functions in the std library are sub-par: they are not object-oriented and they reflect an obsolete, lets-wrap-OS-system-functions-without-thinking philosophy. I'd heartily recommend the 'path' module as a wrapper (around os, os.path, glob and tempfile if you must know): much nicer and OOPy: http://pypi.python.org/pypi/path.py/2.2

This is walk() with the path module:

dir = path(os.environ['HOME'])
for f in dir.walk():
    if f.isfile() and f.endswith('~'):
        f.remove()

Solution 3:

Try "listdir()" in the os module (docs):

import osprintos.listdir('.')

Solution 4:

Straight from Python's Refererence Library

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']

Solution 5:

Take a look at os.walk() and the examples here. With os.walk() you can easily process a whole directory tree.

An example from the link above...

# Delete everything reachable from the directory named in'top',
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import osfor root, dirs, files inos.walk(top, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

Post a Comment for "Looking For File Traversal Functions In Python That Are Like Java's"