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

How to exclude files extension from os.walk

I would like to search files, except those which contain .txt file. How to do that ? At the moment my code is searching for files with .txt extension on it. How to do an opposite? src = raw_input("Enter source disk location: ") src =…
Wiktor
  • 581
  • 1
  • 12
  • 23
3
votes
1 answer

Copying specific files to a new folder, while maintaining the original subdirectory tree

I have a large directory with many subdirectories that I am trying to sort, I am trying to copy specific file types to a new folder, but I want to maintain the original subdirectories. def copyFile(src, dest): try: shutil.copy(src,dest) except…
Darya
  • 43
  • 2
  • 5
3
votes
2 answers

Filter files based on file name

I have a folder that has 4 different kinds of file. For example: Type 1: 00001_a.png Type 2: 00231_b.mat Type 3: 00001_c.jpg Type 4: 00001_c.png How can I filter these files into 4 lists? My current solution can only filter based on file…
trminh89
  • 877
  • 2
  • 10
  • 17
3
votes
2 answers

Python os.walk() umlauts u'\u0308'

I'm on a OSX machine and running Python 2.7. I'm trying to do a os.walk on a smb share. for root, dirnames, filenames in os.walk("./test"): for filename in filenames: print filename matchObj = re.match(…
Tim
  • 147
  • 1
  • 10
3
votes
2 answers

os.walk() not printing/accessing files

I'm trying to use os.walk in order to access all files and all subfolders within a given folder. At first, my code looks like this: for root, dirs, files in os.walk("/home/DataScience"): for dirName in dirs: print dirName However,…
Z R
  • 121
  • 1
  • 7
3
votes
2 answers

Is there a way to retrieve subdirectories in Python without having to iterate over all files?

I'm looking for a way to list the sub-directories contained with the current working directory, however, I haven't been able to find a way that doesn't iterate over all files. Essentially, if I have a folder with a large number of files and 2…
Novark
  • 419
  • 4
  • 15
3
votes
1 answer

Python. Rename files in subdirectories

Could you please help me to modify below script to change the name of files also in subdirectories. def change(): path = e.get() for filename in os.walk(path): for ele in filename: if type(ele) == type([]) and…
MaciejPL
  • 1,017
  • 2
  • 9
  • 16
3
votes
1 answer

Iterate over infinite files in a directory in Python

I'm using Python 3.3. If I'm manipulating potentially infinite files in a directory (bear with me; just pretend I have a filesystem that supports that), how do I do that without encountering a MemoryError? I only want the string name of one file to…
Brōtsyorfuzthrāx
  • 4,387
  • 4
  • 34
  • 56
3
votes
2 answers

How to handle OSX Aliases in Python with os.walk()?

I'm traversing a directory tree using Python 2.7.x, getting the file and directory sizes as it traverses. The problem I'm running into is that it is mistaking alias files for directories, and then throwing an error that there's "No Such File or…
ballofpopculture
  • 741
  • 1
  • 9
  • 18
3
votes
1 answer

Discrepancies with Python os.walk

I've written a script to crawl directories on my system and record file meta data. I've used os.walk to do this. It has worked for the most part, but when running on different machines it returns a different list of files. Right now I'm testing on…
frankV
  • 5,353
  • 8
  • 33
  • 46
3
votes
3 answers

Over-riding os.walk to return a generator object as the third item

While checking the efficiency of os.walk, I created 6,00,000 files with the string Hello (where number is just a number indicating the number of the file in the directory), e.g. the contents of the files in the directory would look…
GodMan
  • 2,561
  • 2
  • 24
  • 40
2
votes
1 answer

Writing a directory tree in a xml file with python lxml

I am trying to read a directory tree to write it in an xml file without too much sucess: # -*- coding: utf-8 -*- """ Created on Tue Jan 31 13:30:22 2012 @author: Jean-Patrick Pommier """ import lxml.etree as et import os '''' Lire l'arboresence…
Jean-Pat
  • 1,839
  • 4
  • 24
  • 41
2
votes
1 answer

Readline feature in Directory Lister class

The below class is a dynamic attribute generating directory walker by Anurag. import os class DirLister(object): def __init__(self, root): self.root = root self._list = None def __getattr__(self, name): try: …
Neeran
  • 1,753
  • 3
  • 21
  • 26
2
votes
2 answers

Mental block with os.walk - want to process a file not the filename string

Trying to implement a little script to move the older log files out of apache (actually using a simple bash script to do this in 'real life' - this is just an exercise to practice using Python). I'm getting the filename as a string as the variable…
user39178
2
votes
1 answer

Why does os.walk() (Python) ignore a OneDrive directory depending on the number of files in it?

I am doing an os.walk() over a certain part of my OneDrive synced folder structure. It all worked fine until recently. Now ALL files from one specific directory are ignored. I tested several possible reasons and narrowed it down to this: The…
windSpiel
  • 23
  • 5