jjnguy
jjnguy

Reputation: 138922

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 Python?

Upvotes: 10

Views: 9337

Answers (9)

Hazim Sager
Hazim Sager

Reputation: 82

Seeing as i have programmed in python for a long time, i have many times used the os module and made my own function to print all files in a directory.

The code for the function:

import os

def PrintFiles(direc):
    files = os.listdir(direc)
    for x in range(len(files)):
        print("File no. "+str(x+1)+": "+files[x])

PrintFiles(direc)

Upvotes: 0

metakermit
metakermit

Reputation: 22341

You can also check out Unipath, an object-oriented wrapper of Python's os, os.path and shutil modules.

Example:

>>> from unipath import Path
>>> p = Path('/Users/kermit')
>>> p.listdir()
Path(u'/Users/kermit/Applications'),
Path(u'/Users/kermit/Desktop'),
Path(u'/Users/kermit/Documents'),
Path(u'/Users/kermit/Downloads'),
...

Installation through Cheese shop:

$ pip install unipath

Upvotes: 1

Max Maximus
Max Maximus

Reputation: 779

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()

Upvotes: 5

giltay
giltay

Reputation: 2025

I'd recommend against os.path.walk as it is being removed in Python 3.0. os.walk is simpler, anyway, or at least I find it simpler.

Upvotes: 2

Bruno Gomes
Bruno Gomes

Reputation: 1144

Use os.path.walk if you want subdirectories as well.

walk(top, func, arg)

        Directory tree walk with callback function.

        For each directory in the directory tree rooted at top (including top
        itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
        dirname is the name of the directory, and fnames a list of the names of
        the files and subdirectories in dirname (excluding '.' and '..').  func
        may modify the fnames list in-place (e.g. via del or slice assignment),
        and walk will only recurse into the subdirectories whose names remain in
        fnames; this can be used to implement a filter, or to impose a specific
        order of visiting.  No semantics are defined for, or required of, arg,
        beyond that arg is always passed to func.  It can be used, e.g., to pass
        a filename pattern, or a mutable object designed to accumulate
        statistics.  Passing None for arg is common.

Upvotes: 2

dmeister
dmeister

Reputation: 35634

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 in os.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.

Upvotes: 25

Joe Skora
Joe Skora

Reputation: 14920

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 os
for root, dirs, files in os.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))

Upvotes: 2

Big Dave Diode
Big Dave Diode

Reputation: 71

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

import os
print os.listdir('.')

Upvotes: 3

florin
florin

Reputation: 14336

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']

Upvotes: 2

Related Questions