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
12
votes
1 answer

os.walk get directory names

I have such file structure: d:\temp\random1\index.html d:\temp\random2\index.html d:\temp\random3\index.html and I want to get paths to list in python. So the output would be: files = ['path': 'd:\temp\random1\index.html', 'directory':…
grotos
  • 493
  • 2
  • 6
  • 11
11
votes
2 answers

Travel directory tree with limited recursion depth

I need to process all files in a directory tree recursively, but with a limited depth. That means for example to look for files in the current directory and the first two subdirectory levels, but not any further. In that case, I must process e.g.…
Byte Commander
  • 6,506
  • 6
  • 44
  • 71
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,625
  • 8
  • 36
  • 47
10
votes
2 answers

os.walk stop looking on subdirectories after first finding

I need to get the first appearance of the repository.config files in a directory and stop looking in the subdirectories. Here is my directory…
radicaled
  • 2,369
  • 5
  • 30
  • 44
9
votes
2 answers

os.walk is returning a generator object. What am I doing wrong?

According to the documentation, os.walk returns a tuple with root, dirs and files in a given path. When I call os.walk I get the following: >>> import os >>> os.listdir('.') ['Makefile', 'Pipfile', 'setup.py', '.gitignore', 'README.rst', '.git',…
SpaDev
  • 149
  • 2
  • 7
9
votes
1 answer

ZIp only contents of directory, exclude parent

I'm trying to zip the contents of a directory, without zipping the directory itself, however I can't find an obvious way to do this, and I'm extremely new to python so it's basically german to me. here's the code I'm using which successfully…
Flux
  • 93
  • 1
  • 4
9
votes
1 answer

I get OSError: [Errno 13] Permission denied: , and os.walk exits

I have a script to report me about all files, in a directory, so that users will be required to erase them (it's a really badly managed cluster, w/o real superuser). When I run the script I get: OSError: [Errno 13] Permission denied: ' ls: :…
Lior
  • 111
  • 2
  • 3
  • 4
9
votes
4 answers

Extending Python's os.walk function on FTP server

How can I make os.walk traverse the directory tree of an FTP database (located on a remote server)? The way the code is structured now is (comments provided): import fnmatch, os, ftplib def find(pattern, startdir=os.curdir): #find function…
warship
  • 2,924
  • 6
  • 39
  • 65
9
votes
2 answers

Get a list of the lowest subdirectories in a tree

I have a path to a certain directory, and I need to get a list of all of it's sub-directories that themselves don't contain any sub-directories. For example, if I've got the following tree: -father_dir: -son_dir1: -grandson.txt …
Cheshie
  • 2,777
  • 6
  • 32
  • 51
9
votes
4 answers

os.walk() ValueError: need more than 1 value to unpack

Alright, I'm working with a Bioloid Premium humanoid robot, and Mac OS X will not recognize it. So I wrote a Python script to detect changes in my /dev/ folder because any connection on a Linux-based system is still given a reference via a file…
Ech0riginal
  • 329
  • 1
  • 3
  • 7
8
votes
3 answers

Efficiently removing subdirectories in dirnames from os.walk

On a mac in python 2.7 when walking through directories using os.walk my script goes through 'apps' i.e. appname.app, since those are really just directories of themselves. Well later on in processing I am hitting errors when going through them. I…
Patrick Bateman
  • 271
  • 2
  • 5
  • 14
7
votes
2 answers

Making os.walk work in a non-standard way

I'm trying to do the following, in this order: Use os.walk() to go down each directory. Each directory has subfolders, but I'm only interested in the first subfolder. So the directory looks like: /home/RawData/SubFolder1/SubFolder2 For example. I…
Z R
  • 121
  • 1
  • 7
7
votes
3 answers

os.walk very slow, any way to optimise?

I am using os.walk to build a map of a data-store (this map is used later in the tool I am building) This is the code I currently use: def find_children(tickstore): children = [] dir_list = os.walk(tickstore) for i in dir_list: …
Joe Smart
  • 751
  • 3
  • 10
  • 28
7
votes
2 answers

Why does python's os.walk() not reflect directory deletion?

I'm attempting to write a Python function that will recursively delete all empty directories. This means that if directory "a" contains only "b", "b" should be deleted, then "a" should be deleted (since it now contains nothing). If a directory…
seanahern
  • 313
  • 3
  • 12
7
votes
2 answers

Python - walk through a huge set of files but in a more efficient manner

I have huge set of files that I want to traverse through using python. I am using os.walk(source) for the same and is working but since I have a huge set of files it is taking too much and memory resources since its getting the complete list all at…
nirvana
  • 195
  • 1
  • 4
  • 13
1
2
3
48 49