A word of warning - the highest rated answer might be susceptible to race conditions.
You might want to perform os. stat instead, to see if the directory both exists and is a directory at the same moment.
Every directory on a unix system (and probably every other system too) contains at least two directory entries.
These are.
(current directory) and.
(parent directory).
In the case of the root directory,.
Is the current directory, while.
Signifies the parent directory.
It makes things quicker at the command line as well so you don't need to type out full paths.
Go up 2 directories:
Use path. glob() to list all files and directories.
And then filter it in a list comprehensions.
Path() is the current working directory, not the directory of the script.
This only works in the few cases where the script actually is in the current working directory.
In bash all i know is that rmdir directoryname will remove the directory but only if it's empty.
Is there a way to force remove subdirectories?