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
6
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…
6
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
  • 11,303
  • 12
  • 38
  • 52
6
votes
3 answers

Python os.walk memory issue

I programmed a scanner that looks for certain files on all hard drives of a system that gets scanned. Some of these systems are pretty old, running Windows 2000 with 256 or 512 MB of RAM but the file system structure is complex as some of them serve…
JohnGalt
  • 419
  • 1
  • 5
  • 16
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
  • 17,833
  • 17
  • 90
  • 133
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
  • 239
  • 1
  • 4
  • 8
6
votes
4 answers

os.walk to crawl through folder structure

I have some code that looks at a single folder and pulls out files. but now the folder structure has changed and i need to trawl throught the folders looking for files that match. what the old code looks like GSB_FOLDER = r'D:\Games\Gratuitous Space…
Mat Gritt
  • 79
  • 1
  • 1
  • 4
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
  • 649
  • 5
  • 21
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
  • 19,525
  • 19
  • 84
  • 147
5
votes
3 answers

What are the empty lists in dirname of os.walk?

When using python 3.5 os.walk() and looking at the dirname output I get a lot of empty lists (returned [ ]). I have no idea why. import os tdir = '/home/pontiac/testdir' gen0 = os.walk(tdir) print(next(gen0)[1]) print(next(gen0)[1]) gen1 =…
user3163030
  • 313
  • 5
  • 12
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
  • 125
  • 2
  • 3
  • 7
5
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…
user3368693
  • 73
  • 1
  • 5
5
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
  • 55
  • 1
  • 5
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
1 answer

Does os.walk leak memory?

When I run this Python script in Windows, the process grows with no apparent end in sight: import os for i in xrange(1000000): for root, dirs, files in os.walk(r"c:\windows"): pass Am I misunderstanding something? (I'm using Python…
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
1 2
3
48 49