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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With