Questions tagged [os.walk]
`os.walk()` is a Python function which serves to walk a directory tree.
726
questions
0
votes
1
answer
40
views
Moving files with subfolders based on dictionary Python
I am trying to figure out a faster way to copy and move pdf files that are in multiple subfolders. The current code I am using will find the file if it is anywhere in the main folder directory but ...
0
votes
0
answers
16
views
Execute a task on subfolders within a folder
I have a folder_X that has multiple subfolders within it. I need to execute my code on all of the files within the subfolder, while keeping them in their respective folders.
I have tried glob and os....
0
votes
1
answer
36
views
Create directory of filenames with zipfolders with Python
I have a folder filled with lots of zipfolders. Inside each zip is between 1 to 4 CSV files. I'd like to create a directory (printed out to excel preferibly) that has in column 1, the number within ...
0
votes
0
answers
27
views
Cannot open file found by os.walk (python 3.7)
I am using os.walk to recursively search through a directory to find a file and parse it for data. Although os.walk is able to find the file, trying to open it yields a FileNotFoundError. It can find ...
1
vote
1
answer
37
views
How can I use os.walk and PIL to save an edited image in a new directory but with the same subdirectories as in the original source?
I'm trying to edit all images within a specified directory (and subdirs) using PIL, and then save the edited versions in a different location but under the same subdirectory structure.
ie. Input:
Root ...
0
votes
0
answers
107
views
How to fix "FileNotFoundError: [Errno 2] No such file or directory"
I'm using the following script to find the hash values of all the files in a directory, but I keep having an issue with some random files giving me the above error. I've tried to solve this with what ...
0
votes
0
answers
17
views
Extract name of main subfolder, create new subfolder, and move files from source to destination
I'm fairly new to python but I'm trying to write a code that essentially looks at all the folders in my source path, extract the names of the folders in the source and create new folders with a ...
0
votes
3
answers
83
views
How can I limit os.walk results for a single file?
I am trying to search a given directory for a specific file, and if that file does not exist I would want the code to say "File does not exist". Currently with os.walk I can get this to work,...
0
votes
0
answers
44
views
Process only files with filename above a certain number
I have a single directory full of millions of files with file names such as e.g.:
234.txt
235.txt
236.txt
I would like to work through the files with a name that has an integer prefix above a certain ...
-2
votes
1
answer
70
views
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&...
1
vote
1
answer
98
views
Why is the order of directories listed by os.walk() the same irrespective of the "topdown" parameter?
I have a dataset in a specific root and trying to iterate through dirs and files, but the topdown parameter does not work as expected.
Images
├── n01440764
│ ├── image1.jpg
│ ├─ ...
│ └── ...
1
vote
0
answers
35
views
How to compare a premade list of files with os.walk generated list
I am attempting to create an automated verification code for folders and files. There are certain files and folders that are required to be in a project to make sure it functions correctly. My code is ...
0
votes
3
answers
705
views
Recursively search a directory, subdirectories, and files and print any with an uppercase letter in Python
I'm trying to create a function that will recursively search a directory, it's subdirectories, files and print out any that contain an uppercase letter. I've searched SO and other fourms for a way to ...
2
votes
1
answer
488
views
Why does os.walk() (Python) ignore a OneDrive directory depending on the number of files in it?
I am doing an os.walk() over a certain part of my OneDrive synced folder structure. It all worked fine until recently. Now ALL files from one specific directory are ignored.
I tested several possible ...
0
votes
0
answers
92
views
Is there a faster way than os.walk() to get the full absolute path of a directory name in Python, if only given the dir name?
I am a beginner in python, and I would like to write a python function that quickly returns the full absolute file path to a directory, if given only the directory name. If it can't find it, I want it ...
0
votes
0
answers
15
views
nested function overwrite my return buffer [duplicate]
i'm trying to write a python sw that takes every file in a defined directory and searches them in an another directory by means of the subdirectories' names in which files are contained. To do so i'm ...
2
votes
2
answers
140
views
How to sort os.walk(path) in alphanumeric order, with duplicates coming after the original file, using Python 3?
In python 3 (specifically 3.10.6), how can you change the way that os.walk(path) sorts the files it finds? Given this list of files:
IMG0001.jpg
IMG0002.jpg
IMG0002(1).jpg
IMG0002(2).jpg
IMG0003.jpg
...
0
votes
1
answer
90
views
Python: equivalent of os.walk for folders that are stored in a git repository
Current code
myfolder = '../../../../../config'
for directory, subdirectories, files in os.walk(myfolder ):
myfolder used to be in the same git repo as this code.
But a refactoring lead to it being ...
1
vote
0
answers
47
views
A script that searches through the text of all sub-directories' files for a string then prints to a created file
I'm a scripting noob and I'm stuck on this problem.
I would like the code to do a few things:
Ask for user input for a string to search for.
Iterate through the sub-directories from a given file path....
0
votes
1
answer
29
views
Finding files only inside a directory based on file extensions and not in sub-directories inside the directory
I need to iterate through different folders and check if certain files are present inside directory based on their file extensions.
However during iteration it is also checking on the subfolders ...
0
votes
1
answer
120
views
iterating through folders and finding files based on file extension using python [duplicate]
I need to iterate through different folders and check if certain files are present inside directory based on their file extensions.
However during iteration it is also checking on the subfolders ...
0
votes
1
answer
65
views
Using Python to copy xlsx files from newest subfold in directory and copy into dynamic folder with matching file name of the xlsx files
copyfrom = 'C:\Users\lexluthorella\Documents\Test_dir'
copyto = 'C:\Users\lexluthorella\Documents\Fake_sftp
The subfolders in the copyfrom directory are as follows:
C:\Users\lexluthorella\Documents\...
0
votes
0
answers
44
views
os.walk() ignore few directories and subdirectories under them
I have a a top level directory and then few subdirectories underneath it which in turn have few subdirectories and files.
the tree view looks like this:-
treeview
├── abc
│ ├── pqr
│ │ ├── 1
│ ...
0
votes
1
answer
87
views
Why do I get a ''FileNotFoundError'' in python?
I have a list of the xlsx files in my directory and subdirectories and I want to loop through this list with certain conditions. Now it seems that the code works for the main directory, but it has ...
1
vote
0
answers
35
views
How do I determine that a folder really exists in a Streamed Google Drive?
I have a Google Drive that had a large number of changes accidentally made to its file and folder structure externally, which I am trying to undo. I am trying to run a Python script on a Windows ...
0
votes
2
answers
43
views
Simple Python program that checks in each subfolder how many files there are and which extensions the file contains
I am writing a simple python script that looks in the subfolders of the selected subfolder for files and summarizes which extensions are used and how many.
I am not really familiar with os.walk and I ...
0
votes
2
answers
218
views
I'm trying to search multiple directories with hundreds of different text files with random text in them, but I'm struggling
I have hundreds of little text files in multiple folders. In each text file is loads of random letters and symbols and I have been tasked with finding certain information like "HSBC" and &...
0
votes
2
answers
254
views
Build JSON object directory tree using Python os.walk
So I'm just hitting a mental roadblock on solving this problem and none of the other questions I have looked at have really captured my particular use-case. One was close but I couldn't quite figure ...
0
votes
0
answers
43
views
Priority in os.walk
I'm trying to find a better way to have os.walk prioritize one type of subdirectory over the other, without running the entire loop twice.
Here's an example:
Say my directory is as follows-
Root
Root/...
0
votes
1
answer
180
views
How can I append a folder directory with many sub directories to a QTreeView using QStandardItemModel in PyQt6
I am building a script that takes a path and gets the folders. Then appends those folder names as items to another item that I am calling "Root". I am using QStandardItemModel and not ...
0
votes
0
answers
51
views
Python os.walk returns weirdly formatted root directory
I've written a program which uses tkinter's askdirectory to get a folder and then convert all images into another format. But I've noticed something weird.
The first step the program does is ask for a ...
0
votes
1
answer
114
views
unzip all .zip and .gz files from the subfolders of a particular folder using python
I was trying to unzip all .zip and .gz files from the subfolders of a particular folder. I want to extract all files in the same subfolder itself. I tried the code below. But I kept getting a file not ...
0
votes
0
answers
156
views
Using os.walk with multiprocessing
I am trying to walk through a large folder / file structure to find all files on a secondary drive (W:\) that contain a specific string.
Doing this using os.walk works, but takes a very long time (>...
0
votes
1
answer
152
views
Iterating through all files in a root folder to search for specific files using os.walk()
I've tried to search previous questions but I couldn't figure out a solution to my problem.
I have a root folder that has many different subfolders and files. Each subfolder also has files within them ...
0
votes
0
answers
47
views
converting text files to csv with filename gives Error
import csv
import pandas as pd
import os
filelocation = "C:\\test"
files= [k for k in next(os.walk(f"{filelocation}"))][2]
filenames = []
content=[]
for file in files:
with ...
0
votes
1
answer
110
views
os.walk loop is not passing the readout of the last directory in the hierarchy to a list
I'm using os.walk and os.path.splitext to generate two separate lists of folder names and file names (w/o extension) located in a given disk drive of a Windows 10 system. For this purpose I'm using ...
1
vote
0
answers
174
views
While obtaining hash files, some folders and files from the directory are not showing up
My code was working just fine before adding the hash function. I was getting the list of all folders and files in my directory in the Pretty Table. Once I added the hash function, I got maybe 5 of ...
0
votes
0
answers
250
views
copy files from bitbucket repository to S3 bucket using python script
I am running a python script (through teamcity) which deploys AWS cloud formation templates. As a pre-requisite all the templates and script files in the bit bucket repository should be copied to S3 ...
0
votes
0
answers
12
views
Want to move subfolder files with specific name to new folder
This is the architecture of my folder I want to select all patients' data weather positive or negative and copy them into the new folder name Poisitive_Img_folder and Negative_Img_folder.
Kindly ...
0
votes
1
answer
169
views
How to stop recursive os.walk on a parent path that contains the first instance of a string from a list, while allowing os.walk to query sibling dirs
I am using os.walk to query a directory tree for directories with names that include any strings from a my_list.
Directory tree:
./user/zebra/
./user/zebra/zebra_01/
./user/zebra/zebra_02/
./user/lion/...
0
votes
1
answer
17
views
How get on same time count list files and files adress?
i need to ask if possible and how get on same time the sum of list files in directory and subdirectory with fnmatch filter and files adress.
I use for now this:
def return_ext():
file_pasok = [&...
-1
votes
1
answer
57
views
Python: Create subdirectories at the end of the tree
Please see my directory tree below.
I need to add an Aug 2022 folder at the end of the tree for each of Subfolder1, 2, and 3.
But these subfolder1, 2, 3 have different levels, for example:
The Aug ...
0
votes
2
answers
493
views
How do I add the directory path as another Column to my dataframe?
I am using this python script to get csv files with particular name in sub-directories of my folder and reading them into dataframe, I have been trying to add their directory paths as another column ...
0
votes
3
answers
36
views
How to show directory name only, instead of whole path in os.walk
So if I want to print only the directory name instead of whole base path, is there any tricks to achieve it :)
import os
for root, dirs, files in os.walk("/home/lungsang/Desktop/levelpack-UI/...
1
vote
0
answers
26
views
Why I got infinity loop to add files in archive?
I started learn python and trying to create 'backup' app. Want to add files from chosen directory in zip archive, but I don't understand why zipfile.write adding the same files from directory in ...
0
votes
0
answers
35
views
How to resume from previous failed iteration while traversing through a big nested directory in Python?
I am currently using os.walk to navigate through all subfolders and files in a massive Network drive directory, However, Whenever my VPN disconnects, The for loop fails. Next day when I re-run my code,...
2
votes
1
answer
189
views
Find leaf folders that aren't hidden folders
I have a folder structure with some epubs and json files in the down-most folders (not counting the .ts folders). I'm exporting tags from the json files to tagspaces, by creating a .ts folder with ...
0
votes
1
answer
34
views
Continue from given folder when walking recursively through folder
I'm walking recursively through a folder using json files that contain metadata to tag some ebook files and creating a .ts folder with other json files. The process has stopped with an error and I don'...
0
votes
1
answer
33
views
Find Files with the term "deadbolt" in it and return only first subfolder with os.walk
This script gets a term and a path to a folder. Its goal is then to search in every subfolder for files that contain the term "deadbolt" in it and make a list and return that list.
So far so ...
1
vote
1
answer
75
views
The os.walk method not work properly after converting to an EXE
I use the os.walk method to get the absolute path of all of the HTML files under the specified path. When I run it as a python script I have no problems, but after converting it to an EXE(Use ...