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)
620 questions
-1
votes
1 answer

How do you have more than 3 items in a for loop with os.walk?

I currently have for current_dir, dirnames, unfilenames in os.walk(input_dir): which works fine. I'd like to be able to store an iteration variable i. for i, current_dir, dirnames, unfilenames in os.walk(input_dir): returns the following error:…
p014k
  • 416
  • 1
  • 5
  • 16
-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
-1
votes
3 answers

os.walk on non C drive directory

I know there are several posts that touch on this, but I haven't found one that works for me yet. I need to create a list of files with an .mxd extension by searching an entire mapped directory. I used this code and it works: import os …
user23469
  • 1
  • 1
-2
votes
1 answer

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

os.walk() does not print out file and folder names

import os mypath = '/Users/ken/Desktop/myFolder/' for folderName, subfolders, filenames in os.walk(mypath): print('The current folder is ' + folderName) for subfolder in subfolders: print('SUBFOLDER OF ' + folderName + ': ' +…
Ken
  • 9
  • 1
-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
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.
-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
  • 419
  • 1
  • 5
  • 10
-2
votes
2 answers

python for loop in os.walk()

i write this code to get the count of files inside my directory but i need it to count the output lines when it run ... import os for dir,subdir,files in os.walk(r"C:\Users\adam\Desktop\test"): i = 0 i = i + 1 print(str(i) + ": files…
evilcode1
  • 103
  • 2
  • 9
-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
  • 83
  • 2
  • 7
-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
  • 633
  • 2
  • 6
  • 16
-3
votes
3 answers

Python class error TypeError: __init__() missing 1 required positional argument: 'self'

I have a python class as following. class copyingfiles(): @staticmethod def __init__(self, x=[], y=[], z=None, i=None): self.x = x self.y = y self. z = z self.i= i @staticmethod def mover(self): …
user1017373
  • 1,738
  • 3
  • 21
  • 38
-3
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
  • 3
  • 2
1 2 3
41
42