I have been working on a CLI tool in Python 3.9 to generate a template project and ran into a strange edge case when the tool is called from a Windows directory that has been deleted.
Instead of raising some sort of error because I called the tool from a path like: C:\Users\...\Projects\a-deleted-folder
, Python sets the working directory to C:\
happily generates files anyways.
For example, my CLI tool is called qtdk
and if I call it in a deleted folder like so:
PS C:\Users\...\Projects\a-deleted-folder> qtdk init
It instead acts like it was called like this:
PS C:\> qtdk init
What I would like to happen, is for this to raise an error since I don't want to start generating files in the C:\
directory if I try to initialize a project in a directory that does not exist.
Is there any way to detect if this is happening and raise an exception?
I've tried checking sys.argv
and os.getcwd()
but neither seem to provide any indication of where the tool was originally called.