FAQ Database Discussion Community


Why does python's os.walk() not reflect directory deletion?

python,os.walk
I'm attempting to write a Python function that will recursively delete all empty directories. This means that if directory "a" contains only "b", "b" should be deleted, then "a" should be deleted (since it now contains nothing). If a directory contains anything, it is skipped. Illustrated: top/a/b/ top/c/d.txt top/c/foo/ Given...

Retrieving specific directories using os.walk()

python,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 than a set time limit but also...

os.walk missing out certain subdirectories

python,operating-system,os.walk
for dirname, dirnames, filenames in os.walk("C:\\",followlinks=True,topdown=True): for subdirname in dirnames: os.chdir(os.path.join(subdirname, dirname)) if os.getcwd()!="C:\Windows\winsxs": print(os.getcwd()) As you can see, this code is supposed to search the entire C drive for all subdirectories and change Python's working directory and display the result. I can't help but notice for whatever reason, os.walk...

Printing final (leaf?) nodes in directory listing Python

python,os.walk
I can walk the directory and print just folder/directory names but I would like to exclude folder names of directories that contain other directories. For some reason I am calling that a "final node" in the tree structure but I could well be fooling myself, wouldn't be the first time....

using os.walk cannot open the file from the list

python-2.7,os.walk
My problem is to read '.csv' files in catalogs and do some calculations on them. I have calculations working but my for loop seem not to work as I want to. d = 'F:\MArcin\Experiments\csvCollection\' for dirname, dirs, files in os.walk(d): for i in files: if i.endswith('.csv'): data1 = pd.read_csv(i, sep=",")...

In Python, how do I remove subdirectories that do NOT contain a certain file extension (ie .pdf)?

python,python-2.7,subdirectories,os.walk
I'm trying to write a program that physically removes all subdirectories of a directory that do not have any .pdf files in them. I think the solution requires os.walk but haven't figured out much. I made a recursive function to do this but I'm not sure if it works def...

List all branches of subfolders in directory with python

python,python-2.7,os.walk
I'm trying to create a list of all folders and sub folders and sub-sub (etc) folders in a directory. So far I've come up with this: roots = list(set([a[0] for a in tuple(os.walk(r"C:\example"))])) It's a bit slow though, mainly due to os.walk when there are many files in the directories....

How can I get a specific folder with a specific folders sibling in python

python,os.walk
I would like to find the path to specific folders that have only specific folders brothers ex: I want to find all folders named : zeFolder with siblings folders brotherOne and brotherTwo |-dad1|---brotherOne|---brotherFour|---zeFolder (not match) |-dad2|---brotherOne|---brotherTwo|---zeFolder (♥♥♥Match♥♥♥) [...] Below is my code, but with this solution I find all the...