Os.getcwd a gamble?

what does os.getcwd return (I’m on Windows) :melting_face:

It returns the current working directory, of course!

os.getcwd() gets the current workinf directory. So on Windows this would be something like r'C:\Users\albert\Desktop'.

Every running program has a cwd, and relative file paths are relative to the cwd. So if you called open('spam.txt') then this refers to C:\Users\albert\Desktop\spam.txt.

If you call os.chdir() to change the cwd to, say, C:\Program Files, then that same open('spam.txt') call would open C:\Program Files\spam.txt.

1 Like

is the cwd a feature of the program or of the user (or of the process)? :disappointed_relieved:

What would it mean for it to be a feature of the user?

I sign in with a user and the cwd is there, so the user is the starting point :sweat_smile:

As a user I can have many different terminals open with many different working directories simultaneously. Which one is the “current” working directory?

Maybe you are thinking of the home directory?

But you’ll have to sign in first and that environment will initially be used for the next terminal, isn’t that so :confused:

Why does it matter where the terminal starts? It’s the current working directory, not the initial working directory. If I change directories, it changes.

Maybe you should define what you’re asking more clearly, and how you define the terms involved. Because it sounds like you are using a different vocabulary from what is standard.

The process.

It’s the process. Each process has a current working directory. If you start a new process it either inherits the parent’s cwd or sets a specific cwd for starting the process. The specifics of how that happens is OS(and multiprocessing start-method) dependent.

So each terminal window/tab will have a different cwd as well. When you run an app from the terminal, it starts in the same current working directory as the terminal was in just before you hit enter to start the new process. The subprocess can change it’s cwd, but that won’t affect its parent (the terminal).

1 Like

well, nice exhaustive answer for a short question, thanks! :joy:
my confusion was the launcher which seems to change the cwd to sys.path[0] :blush: