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
3 answers

Python's os.walk() fails in Windows when there are long filenames

I use python os.walk() to get files and dirs in some directories, but there're files whose names are too long(>300), os.walk() return nothing, use onerror I get '[Error 234] More data is available'. I tried to use yield, but also get nothing and…
user2646309
  • 53
  • 1
  • 4
4
votes
2 answers

Improve efficiency of python os.walk + regular expression algorithm

I'm using os.walk to select files from a specific folder which match a regular expression. for dirpath, dirs, files in os.walk(str(basedir)): files[:] = [f for f in files if re.match(regex, os.path.join(dirpath, f))] print dirpath, dirs,…
RogerFC
  • 329
  • 3
  • 15
4
votes
3 answers

Moving files and creating directories if certain file type in python

This is probably a simple question, but I'm brand new to python and programming in general. I'm working on a simple program to copy/move .mp3 files from on location to another while mirroring the directory structure of the source location. What I…
dkehler
  • 43
  • 1
  • 4
4
votes
1 answer

python storing path names with forward vs backward slash

I have a procedure that os.walks a directory and its subdirectories to filter pdf files, separating out their names and corresponding pathnames. The issue I am having is that it will scan the topmost directory and print the appropriate filename e.g.…
Robert Lear
  • 91
  • 1
  • 3
  • 7
4
votes
1 answer

Os.walk wont work with directory matched with regex

This is the code i used. Why cant os walk handle ? import os, re cwd = os.getcwd() directory= 'Box II' dirpattern = re.compile(r'^.*?\\'+directory+'.*?', re.M) for root, dirs, files in os.walk(os.path.abspath(cwd)): if…
Baf
  • 2,041
  • 3
  • 16
  • 10
4
votes
3 answers

How to improve searching with os.walk and fnmatch

I'm using os.walk and fnmatch with filters to search a pc's hdd for all image files. This works perfectly fine but is extremely slow since it takes about 9 minutes to search +-70000 images. Any ideas on optimizing this code to run faster? Any other…
user1401950
  • 967
  • 2
  • 12
  • 28
3
votes
1 answer

Seeking elegant design of data representation for a directory tree

I'm seeking advice on elegant designs for representing a file directory without symlinks in Python where I can query the relationships in terms of "belongs to" (e.g. G is a subdirectory of /A/B/C). My current thinking goes in this direction: Given a…
Axial
  • 125
  • 7
3
votes
1 answer

Is there a way to find specific sub folders in a directory in Python?

I have a Python program where I'm searching through a directory but for certain sub folders in that directory I want to delete files in them if they were created for more than 7 days and the rest of the files in the directory delete if they were…
Sdot2323
  • 59
  • 5
3
votes
5 answers

How can I ignore or remove ".ipynb_checkpoints" in colab?

My code in tf.keras is given below. I want to retrieve a file(Xscale.npy) in each sub_directory(component_0, component_1) of model_cnn folder. root_dir = '/content/drive/My Drive/DeepCID/model_cnn' i=0 for (root, dirs, files) in…
Dong-Ho
  • 43
  • 1
  • 1
  • 4
3
votes
2 answers

Os.walk() alternatives

I'm writing a program that needs to explore all possible subdirectories of a given path topdown. My problem is that I need to do things before calling recursion and after finish recursion and os.walk() doesn't allow this. More precisely the…
L.A.
  • 243
  • 2
  • 12
3
votes
1 answer

Using os.walk to get all files from dir and its subdirs

I'm currently writing a python script which you can give a drive or folder, and it then iterates through every image on the drive or folder and moves them all into one folder. My issue is that using os.walk, it isn't opening the subdirs…
siroot
  • 193
  • 1
  • 2
  • 9
3
votes
2 answers

Python code performance on big data os.path.getsize

Below is my code to get file size in ascending order. def Create_Files_Structure(directoryname): for path, subdirs, files in os.walk(directoryname,followlinks=False): subdirs[:] = [d for d in subdirs if not d[0] == '.'] try: …
3
votes
1 answer

Python parallel processing to unzip files

I'm new to parallel processing in python. I have a piece of code below, that walks through all directories and unzips all tar.gz files. However, it takes quite a bit of time. import tarfile import gzip import os def unziptar(path): for root,…
Jake
  • 2,482
  • 7
  • 27
  • 51
3
votes
1 answer

How to rename files using os.walk()?

I'm trying to rename a number of files stored within subdirectories by removing the last four characters in their basename. I normally use glob.glob() to locate and rename files in one directory using: import glob, os for file in…
Joseph
  • 586
  • 1
  • 13
  • 32
3
votes
2 answers

os.walk a path with wildcard

I would like to traverse a directory and search for a given file. Here is some code I wrote: import os def find(filename, path): for root, dirs, files in os.walk(path): for file in files: if file==filename: …
Riad
  • 953
  • 3
  • 13
  • 23