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

os.walk does not see files that are in the folder

I used the following code some time ago for a project involving parsing tweets. Now, I want to extract more features associated with those tweets and re-running it on the same files I parsed before; however, receive an unexpected error. The file…
kiton
  • 15
  • 6
-1
votes
2 answers

How do I specify a file that doesn't exist in a path?

I have used this to find a file in a given path: path = "C:\\Users\\derpderp\\" name = "derp.xlsx" for root, dirs, files in os.walk(path): if name in files: print(name) But how do one go about doing the inverse? So that if the file not…
fr1234
  • 15
  • 3
-1
votes
2 answers

In Python, As Iam looping through a file directory how do enter a text file at the root and replace strings that match re with xxxxx

I want to loop through a directory with many files and append strings within a each text file. I can capture the string with re.findall method. I want to replace that string with xxxx within each text file.. import os import pandas as pd import…
user12359215
-1
votes
1 answer

os.walk() function giving stop iteration error

import tkinter as tk from tkinter import * import os master = tk.Tk() listbox=Listbox(master,selectmode=MULTIPLE,width=20,height=10,font=("Calibri", 12),exportselection=0) listbox.pack(side=LEFT,fill="y") tk.Label(master, text="Enter Folder Path…
SaEIHN
  • 17
  • 3
-1
votes
1 answer

How to find and copy the content of sub directories in python?

I have a root folder that has a structure as root A |-30 |-a.txt |-b.txt |-90 |-a.txt |-b.txt B |-60 |-a.txt |-b.txt C |-200 |-a.txt |-b.txt |-300 …
KimHee
  • 728
  • 2
  • 12
  • 22
-1
votes
1 answer

Python: how to move through folder recursively and shutil.move() files to target folders

Beginner here. I want to be able to traverse folders and their subdirectorys and files and move all unique file extensions into a dedicated folder for that filetype. Ex .jpg -> into jpg folder. (This is all in Python's IDLE) I have this code:…
dmc30
  • 1
  • 1
-1
votes
2 answers

Using Python and os.walk to find targets

I am currently working on a script to sift through filesystems (both Linux and Windows), to search for specific strings in text files. With what I have below I can select where I want to start, get the full paths to the files with the extensions I…
-1
votes
1 answer

Os walk directory return string literal for unzipping

The problem occurs where the string variable tries to get read by zipfile.Zipfile(variable). The callback is file does not exist. This is because it is adding an extra \ to each folder. I tried several things to try and make the string into a…
-1
votes
1 answer

FTP OS.Walk goes into endless loop

Simply trying to list files from a remote FTP folder which contains only one file (/public_html/Data/ ['TestFile.txt']). os.walk simply returns the same filename over and over in a endless loop until I don't manually interrupt. Code is: import…
Vivek
  • 19
  • 2
-1
votes
1 answer

Find specific files in subdirs using Python os.walk

In Windows, if I ran: dir NP_???.LAS I get 2 files: NP_123.LAS NP_1234.LAS Using fmatch with NP_????.LAS mask I get only NP_1234.LAS, not NP_123.LAS. Below, the code I´m running: def FindFiles(directory, pattern): flist=[] for root, dirs,…
Mauro Assis
  • 375
  • 1
  • 5
  • 22
-1
votes
2 answers

os.walk(directory) - AttributeError: 'tuple' object has no attribute 'endswith'

I am trying to make a script in python to search for certain type of files (eg: .txt, .jpg, etc.). I started searching around for quite a while (including posts here in SO) and I found the following snippet of code: for root, dirs, files in…
geo1230
  • 242
  • 4
  • 15
-1
votes
1 answer

ioerror errno 13 permission denied: 'C:\\pagefile.sys'

Below is my code, what I am trying to achieve is walking through the OS generating a MD5 hash of every file the code is functional, however, I receive the error in the title "ioerror errno 13 permission denied: 'C:\pagefile.sys'" when I try to run…
dperrie
  • 315
  • 1
  • 2
  • 11
-1
votes
1 answer

How to print only the subdirectory tree using python?

I have a directory which holds many sub-directory and inside each sub-directory i have some more sub-sub-directory. I have a python code which prints and writes the directory and subdirectory in a file. The code : import os file = open("list",…
user5760871
-1
votes
2 answers

Retrieving specific directories using os.walk()

I have a set of jobs (job1, job2 etc) that runs every hour and after they are completed generate folders (session1, session2 etc) which contains the log files. Due to storage limitation, I need a script that can delete the session directories older…
Emmanuel Osimosu
  • 5,625
  • 2
  • 38
  • 39
-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:…
bmikolaj
  • 485
  • 1
  • 5
  • 16