Short answer: in the current working directory.
Long answer:
From https://docs.python.org/2/library/stdtypes.html#bltin-file-objects
File objects are implemented using C’s stdio package
From https://docs.python.org/2/library/functions.html#open
The first two arguments are the same as for stdio‘s fopen(): name is the file name to be opened
So, this is answered by looking at documentation for stdio
:
From http://www.cplusplus.com/reference/cstdio/fopen/
filename
C string containing the name of the file to be opened.
Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system).
"Specifications of the running environment" means that it will interpret it as if you typed the path into where you were running the file from, aka, cwd.
For example, if I have a script located in ~/Desktop/temp.py
that reads:
f = open("log.txt", 'r')
print "success opening"
f.close()
and I have a file located at ~/Desktop/log.txt
, I get the following output:
~/Desktop $ python temp.py
success opening
But if I cd ..
and try again:
~ $ python ~/Desktop/temp.py
Traceback (most recent call last):
File "/home/whrrgarbl/Desktop/temp.py", line 1, in <module>
f = open("log.txt", 'r')
IOError: [Errno 2] No such file or directory: 'log.txt'
Just to verify:
~ $ touch log.txt
~ $ python ~/Desktop/temp.py
success opening
So you can see it is trying to open it relative to the directory I ran the script from, not the directory in which the script is located.