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
6
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
  • 185
  • 4
  • 13
6
votes
3 answers

os.walk() python: xml representation of a directory structure, recursion

So I am trying to use os.walk() to generate an XML representation of a directory structure. I seem to be getting a ton of duplicates. It properly places directories within each other and files in the right place for the first portion of the xml…
Parris
  • 16,312
  • 15
  • 82
  • 125
6
votes
3 answers

Python os.rename and os.walk together

I just wrote a python script to get rid of some annoying suffixes in filenames, here's my code: import os for root, dirs, files in os.walk("path"): for filename in files: if filename.endswith("[AnnoyingTag].mov"): …
Lin Ti-Wen
  • 219
  • 1
  • 3
  • 8
5
votes
2 answers

How to read the file contents from a file?

Using Python3, hope to os.walk a directory of files, read them into a binary object (string?) and do some further processing on them. First step, though: How to read the file(s) results of os.walk? # NOTE: Execute with python3.2.2 import os import…
DrLou
  • 639
  • 5
  • 20
5
votes
1 answer

Using os.walk to read files

I'm trying to access files rooted in subdirectories of a main directory. For this purpose, I am using os.walk(). I am able to successfully reach the file names and am able to store that in a list. However, when I try to open these files using the…
5
votes
2 answers

Avoiding infinite recursion with os.walk

I'm using os.walk with followlinks=True, but I hit a place where a symbolic link refers to it's own directory, causing an infinite loop. The culprit in this case is /usr/bin/X11 which list listed as follow : lrwxrwxrwx 1 root root 1 Apr…
Eric
  • 18,532
  • 17
  • 78
  • 138
5
votes
2 answers

multicpu bzip2 using a python script

I want to quickly bzip2 compress several hundred gigabytes of data using my 8 core , 16 GB ram workstation. Currently I am using a simple python script to compress a whole directory tree using bzip2 and an os.system call coupled to an…
harijay
  • 8,463
  • 11
  • 34
  • 47
5
votes
2 answers

How to skip directories in os walk Python 2.7

I have written an image carving script to assist with my work. The tool carves images by specified extention and compares to a hash database. The tool is used to search across mounted drives, some which have operating systems on. The problem I am…
user3450524
  • 105
  • 1
  • 3
  • 7
5
votes
1 answer

os.walk() never returns when asked to print dirpaths

I have a simple directory structure: rootdir\ subdir1\ file1.tif subdir2\ file2.tif ... subdir13\ file13.tif subdir14\ file14.tif If I call: import os print…
ciph345
  • 51
  • 3
5
votes
3 answers

Python Walk, but Thread Lightly

I'd like to recursively walk a directory, but I want python to break from any single listdir if it encounters a directory with greater than 100 files. Basically, I'm searching for a (.TXT) file, but I want to avoid directories with large DPX image…
Jamie
  • 113
  • 1
  • 5
4
votes
3 answers

Find all files with os.walk for a specific directory only

A directory tree looks like this: DirA-- | -- Map | -- Fig-- | --file.png | -- Data-- | -- file.xls | -- file.csv There are multiple…
olyashevska
  • 387
  • 2
  • 12
4
votes
2 answers

Targeting a directory with os.walk

Due to a large and convoluted directory structure, my script is searching too many directories: root-- | --Project A-- | -- Irrelevant -- Irrelevant -- TARGET | …
mk8efz
  • 1,134
  • 3
  • 14
  • 32
4
votes
1 answer

os.walk stuck in an infinite loop

I am trying to write a script that navigates through a directory and identifies DICOM files with a specified piece of header information. After I have found a file that meets the criteria, I want to add the directory of the file to a list if it…
4
votes
4 answers

os.walk() not picking up my file names

I'm trying to use a python script to edit a large directory of .html files in a loop. I'm having trouble looping through the filenames using os.walk(). This chunk of code just turns the html files into strings that I can work with, but the script…
user3087978
  • 45
  • 1
  • 5
4
votes
1 answer

Iterating through individual files in os.walk in Python in an idiomatic fashion

I started with some code I got from another stackoverflow question to generate full paths for all the files in a directory tree: import os def recursive_file_gen(mydir): for root, dirs, files in os.walk(mydir): for file in files: …
kuzzooroo
  • 4,854
  • 7
  • 33
  • 70
1 2
3
41 42