open() function python default directory

The default location is the CWD (Current Working Directory), so if you have your Python script in c:\directory and run it from there, if you call open() it will attempt to open the file specified in that location.


The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, the CWD will be whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, the CWD is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.

In any case, your program can query the current working directory by calling os.getcwd().


If you working on Windows OS first type

import os

then type

os.getcwd()

and it should print the current working directory.


os.getcwd()

Shows the current working directory, that's what open uses for for relative paths. You can change it with os.chdir.

Tags:

Python