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

Python - WindowsError: [Error 2] the system could not find the file specified: ... afterusign os.path.getsize

import os import sys rootdir = sys.argv[1] print os.path.abspath(rootdir) with open('output.txt','r') as fout: for root, subFolders, files in os.walk(rootdir): for file in files: path = os.path.abspath(file) print path …
MarianM
  • 3
  • 2
-2
votes
2 answers

how to get an exact directory through os.walk in python

I have a directory that contains directories like /sample1, /sample10, /sample11 etc. when I am using os.walk to access all of them one by one, I am facing some difficulties. for root, dirs, files in os.walk(mypath): if 'sample1' in root: …
Dolly
  • 1
-2
votes
1 answer

How to get a list of all parents in directory that their children are files in python?

I have a tree of directories that in the end of the each directory will always be 2 files, one as suffix ".txt" and one will always be ".jpg", for example: "rootFolder/a/a.txt", "rootFolder/a/b.jpg" "rootFolder/b/c/a.txt", "rootFolder/b/c/b.jpg" and…
Dump Eldor
  • 92
  • 1
  • 11
-2
votes
2 answers

How to iterate through folder and get certain files from the subfolders grouped?

I have a folder that contains several subfolders, each containing 3-4 files that I need. I am trying to iterate through that folder and put all the files from each subfolder in a dictionary that is later dumped in a json file. So far I've managed to…
-2
votes
3 answers

Python - os.walk (searching files in file explorer) doesn't work for me

What is the problem When I try to run a python script that is suppose to find files in my C drive it returns with an error. I tried python 2.7 and 3.8 (I need python 2.7 so I thought that maybe my python was too old but I tried on the latest version…
user11322796
-2
votes
1 answer

How can I break this loop once the file I want in os.walk() is found?

I am using an os.walk() loop and if statement to find the path of a file. It works, however after the path is found and printed the loop doesn't break for a few seconds after this. I want to break the loop after the path I want is printed. New to…
Los
  • 67
  • 5
-2
votes
1 answer

Creating List of images from a list of images

I have a list(display_train_set)containing 200 images. which i made using code below. I now need to make a list containing 40 list, where each list has 5 images from display_train_images. Please help guys i am new to python. dataset_dir =…
-2
votes
2 answers

Finding a filename with zip in the following folder/ directory and extracting it to a new folder in the same directory?

I have used os.walk() to get the list of files to a DataFrame. Now I want to extract the zip folders from the list of files in the DataFrame. DataFrame file_name base_name extension absolute_path rel_path file_1.pdf file_1 …
Deepak Harish
  • 123
  • 2
  • 7
-2
votes
1 answer

Recursively searching both file and subfolder names for a list of strings

I am trying to search (using Python/R/Unix/Bash whatever) for a list of banned words in file and folder names on my network drives. I can use TreeSize but hoping there's a way to script it so it can be automated.
Poola Tony
  • 23
  • 2
-2
votes
1 answer

Ignore . (dot) directories in os.walk

Learning a python I'm working on a generic utility script to recurse any directories below and spit filenames and other properties into a file - on Windows right now. I keep getting errors as below and I don't care about these files which appear to…
JohnZastrow
  • 449
  • 1
  • 5
  • 12
-2
votes
1 answer

placement of break and return statements in python

I am trying to write a function that returns the path of the first file found in a nest of folders. What I have so far is : def dicom_name(rootDir): for dirName, subdirList, fileList in os.walk(rootDir): for f in fileList: …
Liz Z
  • 85
  • 1
  • 8
-2
votes
1 answer

python function read file from subdirectory

I'm trying to write this function so that I can pass files or folders and read from them using pandas. import pandas as pd import os path = os.getcwd() path = '..' #this would be root revenue_folder = '../Data/Revenue' random_file =…
Krownose
  • 13
  • 2
-2
votes
2 answers

Using a raw string with os.walk()

I'm trying to get os.walk() to work in a program I'm working on, but I keep getting the error: ValueError: invalid \x escape From looking around online, I've seen that the error can arise from not using a raw string. However, I still keep getting…
Schack
  • 833
  • 2
  • 7
  • 18
-3
votes
2 answers

Is there a way to improve this CODE? (python os.walk os.pathbase)

The code can run normally, but I feel the "output_file" is too long, so any way improve it? #load input files from input_dir(include child folders) #and create output files to output_dir(include SAME child folders) for root,dirs,files in…
Junior
  • 27
  • 7
-3
votes
1 answer

os.walk renaming folders which contain a '.'

I use the following code to replace file/folder names in a directory. old = abc new = def for path, subdirs, files in os.walk(folder_path): for name in files: if old in name: file_path = os.path.join(path, name) …
Spatial Digger
  • 1,883
  • 1
  • 19
  • 37
1 2 3
48
49