-1
    import os
    dir_name = "new_dir"
    os.mkdir(dir_name)
    os.chdir(dir_name)
    print("THE cwd now is in " + os.getcwd())
    os.rmdir("../"+dir_name)
    print(dir_name+" deleted")
    print("Checking CWD")
    print("THE script now is in " + os.getcwd())

the getcwd returns error instead of checking the current working directory can someone point me to the reason for that as documentation or just tell me why

the output

  • 2
    You make a folder, go into the folder, then delete the folder. This behaves as expected - `os.getcwd` can't give you the current directory because you've just deleted it. – shayaan Feb 17 '21 at 23:04

1 Answers1

2

You were in a particular directory, then deleted it out from under yourself, leaving yourself with a "working directory" that doesn't exist. Don't do that. Leave the directory before deleting it, or weird stuff will happen in cases like this.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271