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?

Asked by: Michael374 | Posted: 28-01-2022

Answer 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 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.

Answered by: Aldus828 | Posted: 01-03-2022

Answer 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('~'):

Answered by: First Name619 | Posted: 01-03-2022

Answer 3

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

import os
print os.listdir('.')

Answered by: Rafael103 | Posted: 01-03-2022

Answer 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')

Answered by: Carlos333 | Posted: 01-03-2022

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

Answered by: Victoria684 | Posted: 01-03-2022

Answer 6

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.

Answered by: Cadie767 | Posted: 01-03-2022

Answer 7

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.

Answered by: Roman341 | Posted: 01-03-2022

Answer 8

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


>>> from unipath import Path
>>> p = Path('/Users/kermit')
>>> p.listdir()

Installation through Cheese shop:

$ pip install unipath

Answered by: Sarah716 | Posted: 01-03-2022

Answer 9

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


Answered by: Victoria145 | Posted: 01-03-2022

Similar questions

How do I call the functions for when I create the character class - Python - This is for a quiz game but using a tree traversal

#here I want to define the self.__tree but I am not sure how to do so, how would I create the tree? oijh0ij0ihoihihoihou

python - Good graph traversal algorithm

Abstract problem : I have a graph of about 250,000 nodes and the average connectivity is around 10. Finding a node's connections is a long process (10 seconds lets say). Saving a node to the database also takes about 10 seconds. I can check if a node is already present in the db very quickly. Allowing concurrency, but not having more than 10 long requests at a time, how would you traverse the graph to gain the highest cove...

python - Networkx node traversal

Using Python's Networkx library, I created an undirected graph to represent a relationship network between various people. A snippet of my code is below: import networkx as nx def creategraph(filepath): G=nx.Graph() #All the various nodes and edges are added in this stretch of code. return G From what I understand, each node is basically a dictionary. The problem that this pres...

python - Are there any problems with this symlink traversal code for Windows?

In my efforts to resolve Python issue 1578269, I've been working on trying to resolve the target of a symlink in a robust way. I started by using GetFinalPathNameByHandle as recommended here on stackoverflow and by

traversal - Python - Write a function that takes a string as an argument and displays the letters backward, one per line

This is an exercise from "How to think like a Computer Scientist". I am learning Python/programming and I'm not sure how to do accomplish this task. Here is an example in the book that displays the letters forwards, I can't figure out how to get the opposite effect. Has to use a while-loop. fruit = 'banana' index = 0 while index > len(fruit): letter = fruit[index] print letter...

pygame - (Python) List traversal in a nested for-loop?

I can't figure out what's happening here. I've got a few lists set up, one for each individual color with corresponding RGB values as its member, and then the list, colors[], that contains each individual color list. Then I've got a nested for loop: the outer loop creates columns of color-filled rectangles and the inner loop advances the row. Not complicated, as I see it. I'm trying to use numdown to traverse the...

python - Change the view prefix in pyramid traversal from "@@" to "+"

I am looking at moving a web app from pylons to pyramid (formally repoze.bfg) because traversal will fit my app much better than url dispatch. Currently, when I have a obj with a number of views, I have the view names prefixed with a '+' in the url. e.g.: /path/to/obj/ (default view...

traversal - How to traverse Rtree 0.6.0 in Python

Does anyone know how to traverse the whole tree in this Python Rtree library? I've checked all its methods but failed to find a interface for this. Any help would be appreciated.

python - mixing url dispatch and traversal

I've been working on an app that uses url dispatch. I setup my root factory based on some great info found here: https://github.com/mmerickel/pyramid_auth_demo (thanks Michael!) Now I'm trying to use pyramid_formalchemy as well. It seems pyramid_formalchemy uses traversal for determining authorization. That's ok, but I'm stuck on one poi...

python - Pyramid Traversal & URL Dispatch & Differing Roots

Having understood Traversal and the whole resource and context concepts from my question, I tried some tutorial examples that played with the Hybrid routing as stated in the documentation. I kind of understand it if not for some minor problems: If I were to traverse the following URL: http://example.com/product/123/ed...

python - Defining a fallback view in traversal

Consider the following Pyramid application: from pyramid.config import Configurator class Foo(dict): pass def make_root(request): return {'foo': Foo()} def foo(request): return request.subpath def bar(request): return {"whoami": "bar", "subpath": request.subpath} def start(global_config, **settings): config = Configurator(settings=settings) config.set_root_factory(make_root) ...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python