Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect last subdir os.walk()

I need to copy similar config files to the very end of each random depth sub tree. I'm using os.walk() to get dirName and subdirlist, but can't get how to ensure of copying to last subdir only. example: tree dir/sd1/sd2 dir/sd3/sd4/sd5 dir/sd6/sd7/sd8/sd9/sd10

there are hundreds of subdirs, dir names are pretty random, I use them to change few lines in a config file(I use fileinput library there without problem, to replace few lines in template). how to filter out only path till the end and copy only in sd2,sd5,sd10? I tried with top-down option too but did not succeed.

like image 502
Drako Avatar asked Jan 24 '26 23:01

Drako


1 Answers

It seems to be quite easy with os.walk() API:

import os

for root, dirs, files in os.walk('.'):
    if not dirs:
        print(root, "is a directory without subdirectories")
        # do whatever you need to do with your files here
like image 111
Łukasz Rogalski Avatar answered Jan 26 '26 13:01

Łukasz Rogalski