0

I found, that to proceess files given by filename I need to do

import os
dir = os.path.dirname(__file__)
filename = os.path.join(dir, '/relative/path/to/file/you/want')

or

import os
dir = os.getcwd()
filename = os.path.join(dir, '/relative/path/to/file/you/want')

But if I do

filename = 'myfile.txt'

then where it will be look for this file?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • http://stackoverflow.com/questions/918154/relative-paths-in-python may be helpful to look at. – user812786 Feb 05 '16 at 18:40
  • Possible duplicate of [os.getcwd() vs os.path.abspath(os.path.dirname(\_\_file\_\_))](http://stackoverflow.com/questions/11274040/os-getcwd-vs-os-path-abspathos-path-dirname-file) – l'L'l Feb 05 '16 at 18:46
  • A path that begins with "/" is not a relative path. – Bryan Oakley Feb 05 '16 at 18:48

2 Answers2

0

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.

user812786
  • 4,302
  • 5
  • 38
  • 50
0

Created a simple python script (open.py) and ran it with strace.

The script:

#!/usr/bin/env python

with open('myfile.txt', 'r') as fd:
    pass

Strace command: strace ./open.py

This showed me (showing only relevant portion):

read(3, "#!/usr/bin/env python\n\nwith  ope"..., 4096) = 70
read(3, "", 4096)                       = 0
brk(0x23a2000)                          = 0x23a2000
close(3)                                = 0
munmap(0x7f7f97dc3000, 4096)            = 0
open("myfile.txt", O_RDONLY)            = -1 ENOENT (No such file or directory)
write(2, "Traceback (most recent call last"..., 35Traceback (most recent call last):
) = 35
write(2, "  File \"./open.py\", line 3, in <"..., 40  File "./open.py", line 3, in <module>
) = 40

Looking at the open system call got the path as it was provided in the python script. open will try to open the file in current working directory, which is where the program was run from.

Ajay M
  • 2,490
  • 1
  • 15
  • 22