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

fnmatch how exactly do you implement the match any chars in seq pattern

so i have a os.walk code search = self.search.get_text() top = '/home/bludiescript/tv-shows' for dirpath, dirnames, filenames in os.walk(top): for filename in filenames: if fnmatch.fnmatch(filename, search) print os.path.join([dirpath,…
user961559
  • 109
  • 1
  • 3
  • 9
4
votes
2 answers

How to get all directories containing specific file limited by depth in Python?

What I'm trying to do I'm trying to get a list of subdirectories up to depth of x containing a file y, such as the following: root/ root/one/ root/one/README.md root/two/subdir/ root/two/subdir/README.md root/three/ In the above tree, I'm trying to…
ctwheels
  • 21,901
  • 9
  • 42
  • 77
4
votes
2 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
  • 427
  • 4
  • 18
4
votes
2 answers

Where does the function for os.walk onerror return to? How do I add more arguments to the onerror function call?

Im writing a program to walk a filesystem using os.walk. I have my for loop using os.walk, and onerror function as follows: def walk_error(os_error): return(os_error) def main(): for root, dirs, files in os.walk('/var/spool/cron/',…
4
votes
1 answer

using os.remove() in os.walk() for loop returns FileNotFoundError

I'm using Python 3.6.4 in the Anaconda command prompt. I have a function that uses os.walk() to loop through all the available files in a root directory. My code is: def apply_to_files(pattern, base='.') : regex = re.compile(pattern) matches…
logos_164
  • 736
  • 1
  • 13
  • 31
4
votes
2 answers

os.walk with regex

I'd like to get a list of files that apply to a regex that i have. I guess i should use os.walk, but how can i use it with regex? Thanks.
Alex
  • 165
  • 1
  • 3
  • 6
4
votes
1 answer

Python unexpectedly moving files with os.rename

I have a script that: Loops through all the files in a directory + its subdirectories Creates folder for each unique year in the list of files Moves files to their respective folder years Renames them based on timestamp + unique number. When I run…
tbd_
  • 1,058
  • 1
  • 16
  • 39
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,374
  • 4
  • 20
  • 36
4
votes
1 answer

Python os.walk skip directories with specific name instead of path

So I have a file system that I want to be able to check and update using python. my solution was os.walk but it becomes problematic with my needs and my file system. This is how the directories are laid out: Root dir1 subdir 1 2 …
Jack Bird
  • 187
  • 3
  • 13
4
votes
2 answers

os.walk error of unhandled stopIteration

I have written a python script and wanted to debug it using eric ide. When I was running it, an error popped up saying unhandled StopIteration My code snippet: datasetname='../subdataset' dirs=sorted(next(os.walk(datasetname))[1]) I am new to…
RaviTej310
  • 1,635
  • 6
  • 25
  • 51
4
votes
3 answers

pysmb to get directory tree of a smb share server

i manage to connect and access a smb share server using pysmb. what i meant is to read/write/delete/create files/folders to/from the server. majority of the time i need to read file ( be it jpg or csv and etc) from the server base on the smb device…
pinky
  • 382
  • 1
  • 5
  • 17
4
votes
2 answers

os.walk() vs os.scandir()

os.scandir claims to be a better directory iterator and faster than os.walk(). It became a part of the stdlib for Python 3. Working in production environments, what are the things to consider when moving from os.walk() to os.scandir()?
user1767754
  • 23,311
  • 18
  • 141
  • 164
4
votes
2 answers

Printing final (leaf?) nodes in directory listing Python

I can walk the directory and print just folder/directory names but I would like to exclude folder names of directories that contain other directories. For some reason I am calling that a "final node" in the tree structure but I could well be fooling…
Dee
  • 191
  • 1
  • 11
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
  • 6,788
  • 11
  • 46
  • 84
4
votes
1 answer

Batch conversion of .dbf to .csv in Python

I have ~300 folders with .dbf files that I would like to convert to .csv files. I am using os.walk to find all the .dbf files and then a for loop utilizing the dbfpy module to convert each .dbf file to a .csv. It seems to be finding and reading the…
5tanczak
  • 161
  • 1
  • 2
  • 8