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)
726 questions
256
votes
13 answers

How to do a recursive sub-folder search and return files in a list?

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. It's currently set as follows: for root, subFolder, files in os.walk(PATH): for item in…
user2709514
  • 2,561
  • 2
  • 13
  • 3
238
votes
14 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
  • 2,577
  • 2
  • 15
  • 19
135
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
  • 10,326
  • 11
  • 40
  • 46
84
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
  • 6,211
  • 15
  • 57
  • 80
53
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
  • 13,194
  • 14
  • 74
  • 96
51
votes
4 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
  • 5,790
  • 1
  • 24
  • 37
49
votes
6 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
  • 13,194
  • 14
  • 74
  • 96
47
votes
5 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
  • 17,651
  • 32
  • 84
  • 118
45
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
  • 833
  • 2
  • 7
  • 18
43
votes
3 answers

Can I force os.walk to visit directories in alphabetical order?

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
  • 721
  • 2
  • 7
  • 18
20
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
  • 2,156
  • 4
  • 17
  • 39
16
votes
1 answer

How to synchronize two folders using python script

Currently, I am working on a project in which am synchronizing two folders. My folders in the following example names ad Folder_1 as source and Folder_2 as destination I want to do the following things. If files which are present in Folder_1 are…
Saqib Shakeel
  • 615
  • 2
  • 8
  • 17
15
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
  • 720
  • 2
  • 8
  • 16
15
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
  • 98,345
  • 23
  • 131
  • 206
14
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,582
  • 4
  • 17
  • 31
1
2 3
48 49