Questions tagged [os.walk]

`os.walk()` is a Python function which serves to walk a directory tree.

With os.walk() you can walk a directory tree and get, for each of the subdirectories, a list of file names.

A simple example goes like

import os
for root, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        print os.join(root, filename)
620 questions
178
votes
13 answers

Using os.walk() to recursively traverse directories in Python

I want to navigate from the root directory to all other directories within and print the same. Here's my code: #!/usr/bin/python import os import fnmatch for root, dir, files in os.walk("."): print root print "" for items…
Nitaai
  • 1,977
  • 2
  • 13
  • 15
167
votes
12 answers

Recursive sub folder search and return files in a list python

I am working on a script to recursively go through subfolders in a mainfolder and build a list off a certain file type. I am having an issue with the script. Its currently set as follows for root, subFolder, files in os.walk(PATH): for item in…
user2709514
  • 1,671
  • 2
  • 10
  • 3
112
votes
21 answers

os.walk without digging into directories below

How do I limit os.walk to only return files in the directory I provide it? def _dir_list(self, dir_name, whitelist): outputList = [] for root, dirs, files in os.walk(dir_name): for f in files: if os.path.splitext(f)[1] in…
Setori
  • 9,286
  • 11
  • 38
  • 45
73
votes
3 answers

In what order does os.walk iterates iterate?

I am concerned about the order of files and directories given by os.walk(). If I have these directories, 1, 10, 11, 12, 2, 20, 21, 22, 3, 30, 31, 32, what is the order of the output list? Is it sorted by numeric values? 1 2 3 10 20 30 11 21 31 12 22…
Vahid Mirjalili
  • 5,363
  • 15
  • 47
  • 73
44
votes
8 answers

Filtering os.walk() dirs and files

I'm looking for a way to include/exclude files patterns and exclude directories from a os.walk() call. Here's what I'm doing by now: import fnmatch import os includes = ['*.doc', '*.odt'] excludes = ['/home/paulo-freitas/Documents'] def…
Paulo Freitas
  • 11,380
  • 13
  • 68
  • 93
43
votes
3 answers

os.walk without hidden folders

I need to list all files with the containing directory path inside a folder. I tried to use os.walk, which obviously would be the perfect solution. However, it also lists hidden folders and files. I'd like my application not to list any hidden…
unddoch
  • 4,105
  • 1
  • 19
  • 30
40
votes
5 answers

Non-recursive os.walk()

I'm looking for a way to do a non-recursive os.walk() walk, just like os.listdir() works. But I need to return in the same way the os.walk() returns. Any idea? Thank you in advance.
Paulo Freitas
  • 11,380
  • 13
  • 68
  • 93
36
votes
4 answers

Quicker to os.walk or glob?

I'm messing around with file lookups in python on a large hard disk. I've been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to be quicker (for usual size directories). Has anyone got any experience with…
joedborg
  • 15,097
  • 30
  • 72
  • 112
35
votes
3 answers

Can I force python3's os.walk to visit directories in alphabetical order? how?

I would like to know if it's possible to force os.walk in python3 to visit directories in alphabetical order. For example, here is a directory and some code that will walk this directory: ryan:~/bktest$ ls -1…
ryan_m
  • 631
  • 1
  • 7
  • 17
35
votes
3 answers

Need the path for particular files using os.walk()

I'm trying to perform some geoprocessing. My task is to locate all shapefiles within a directory, and then find the full path name for that shapefile within the directory. I can get the name of the shapefile, but I don't know how to get the full…
Schack
  • 633
  • 2
  • 6
  • 16
14
votes
4 answers

Python equivalent of find2perl

Perl has a lovely little utility called find2perl that will translate (quite faithfully) a command line for the Unix find utility into a Perl script to do the same. If you have a find command like this: find /usr -xdev -type d -name '*share' …
dawg
  • 80,841
  • 17
  • 117
  • 187
13
votes
4 answers

os.walk multiple directories at once

Possible Duplicate: How to join two generators in Python? Is there a way in python to use os.walk to traverse multiple directories at once? my_paths = [] path1 = '/path/to/directory/one/' path2 = '/path/to/directory/two/' for path, dirs, files in…
John P. Neumann
  • 680
  • 1
  • 8
  • 16
12
votes
10 answers

How to get progress of os.walk in python?

I have a piece of code which I'm using to search for the executables of game files and returning the directories. I would really like to get some sort of progress indicator as to how far along os.walk is. How would I accomplish such a thing? I…
ThantiK
  • 1,454
  • 3
  • 17
  • 31
11
votes
1 answer

Using endswith with case insensivity in python

I have a list of file extensions and I have to write if conditions. Something like ext = (".dae", ".xml", ".blend", ".bvh", ".3ds", ".ase", ".obj", ".ply", ".dxf", ".ifc", ".nff", ".smd", ".vta", ".mdl", ".md2", ".md3" …
edyvedy13
  • 1,612
  • 1
  • 12
  • 30
10
votes
2 answers

python listing dirs in a different order based upon platform

I am writing and testing code on XPsp3 w/ python 2.7. I am running the code on 2003 server w/ python 2.7. My dir structure will look something like…
ccwhite1
  • 3,105
  • 7
  • 31
  • 47
1
2 3
41 42