1

enter image description here

Suppose I am currently at a directory named current_dir which has the directory structure in linux as shown above. This directory has sub directories which further have sub directories and so on.

I want to create a file at the end of last level of each directory, i.e., at directories b and c which are the last level directories in first and similarly at directories e and f which are the last level directories in second. I am given the path to the current_dir directory only.

I was trying out with this code:-

path = os.walk('/current_dir')
for root, directories, files in path:
    for directory in directories:

But the problem here is that I am not able to figure out how to use the directory variable inside the 2nd for loop to check whether it is the last directory or not. How could I do that?

zester
  • 165
  • 3
  • 12

2 Answers2

2

The directories list returned from os.walk() is a list of directories names that exist in the current directory. So if there are no directories in the current path, it will be empty.

path = os.walk('/current_dir')

for root, directories, files in path:
    if  not directories:  #you can check whether a list is empty like that
        #create your file in the current path you checked, it is stored in root variable.
        pass
        
       
    

EDIT: My bad, the not operator in python is "not". I wrote "!" accidentally.

The Niv
  • 135
  • 1
  • 7
1

Here's an approach using the built-in module pathlib:

  • Iterates all files recursively from a target directory with a Path instance.
  • Ignore all non-directories.
  • If the parent of the Path is already in the list then we remove that parent.
  • Add Path itself to list.
import pathlib

paths = []
for path in pathlib.Path("current_dir").rglob("*"):
    if path.is_dir():
        if path.parent in paths:
            paths.remove(path.parent)
        paths.append(path)

Giving you the list paths containing Paths to every 'last-level' folder

Mandera
  • 2,647
  • 3
  • 21
  • 26
  • Why does he need to create a list of the paths? can't he just create a file inside it while in the for loop? I'm pretty sure he can – The Niv Dec 16 '20 at 08:13
  • He doesn't 'need' to do anything, this is just a proof of concept, he can change it however he'd like – Mandera Dec 16 '20 at 08:20
  • The list is needed to keep track of all directories, allowing us to remove previously added but newly discovered to not be 'last-level' directory paths while we are iterating – Mandera Dec 16 '20 at 08:27
  • Well ok but my answer is less general and less complicated. You can check this post:https://stackoverflow.com/questions/10989005/do-i-understand-os-walk-right Next time just please be more careful with your downvotes for low reputation accounts because it has bad effects on new users – The Niv Dec 16 '20 at 08:28
  • Sure your answer is less general and less complicated, but it's wrong. The user's score should never be a factor for voting. I'm sorry that you feel upset over this, that's not my intent. If you thoroughly read the comment I made and edit your answer then I will change my vote – Mandera Dec 16 '20 at 08:32