Often we need to read from, or write to, files in directories that are local to our script. For instance, we could use the below code to save to file all possible values that can be stored in one byte:

my_bytes = bytearray(list(range(0,256)))

with open("output/local.bin", 'wb') as f:
    f.write(my_bytes)

Our script, local_file.py, is located in the following directory:

/home/python
[wintermute@hive python]$ cd /home/python
[wintermute@hive python]$ ls
local_file.py  output

Now, let's say we launch this script from within its location:

[wintermute@hive python]$ python local_file.py

Check if our output file exists, and what is its content:

[wintermute@hive python]$ cd output/
[wintermute@hive output]$ xxd local.bin
0000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f  ................
0000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f  ................
0000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f   !"#$%&'()*+,-./
0000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f  0123456789:;<=>?
0000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f  @ABCDEFGHIJKLMNO
0000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f  PQRSTUVWXYZ[\]^_
0000060: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f  `abcdefghijklmno
0000070: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f  pqrstuvwxyz{|}~.
0000080: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f  ................
0000090: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f  ................
00000a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf  ................
00000b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf  ................
00000c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf  ................
00000d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf  ................
00000e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef  ................
00000f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff  ................

So as we can see, it worked like a charm. But now, let's say we want to launch this script from a different directory than the one where the script is located:

[wintermute@hive ~]$ pwd
/home/wintermute
[wintermute@hive ~]$ python /home/python/local_file.py
Traceback (most recent call last):
  File "/home/python/local_file.py", line 12, in 
    with open("output/local.bin", 'wb') as f:
IOError: [Errno 2] No such file or directory: 'output/local.bin'

We get an IOError. But why?

Turns out we used a relative path when providing the desired location of the output file. There is no directory called "output" in our current location. Needless to say, this is probably not what we wanted to achieve.

How can we fix this problem then? One solution would be to use an absolute path when opening a file, e.g.

with open("/home/python/output/local.bin", 'wb') as f:

But what if the script is moved, or we don't know where it will be stored in the first place? It so happens that module os can help us here.

Using os.path.realpath we can get the exact location of the script:

print(os.path.realpath(__file__))
/home/python/local_file.py

For our needs we only need to know the directory where the script is located, we can get this info using os.path.split:

print(os.path.split(os.path.realpath(__file__))[0])
/home/python

Now, we just need to join this directory with the desired location of our output file:

out_file_path=os.path.realpath(os.path.join(
	module_path,"output/local.bin"))

print(out_file_path)
/home/python/output/local.bin

And that's it, our script should work regardless of the way it is executed.

[wintermute@hive ~]$ pwd
/home/wintermute
[wintermute@hive ~]$ python /home/python/local_file.py
[wintermute@hive ~]$ xxd /home/python/output/local.bin | head -n 5
0000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f  ................
0000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f  ................
0000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f   !"#$%&'()*+,-./
0000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f  0123456789:;<=>?
0000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f  @ABCDEFGHIJKLMNO

Full script:

import os

my_bytes = bytearray(list(range(0,256)))

out_file_path=os.path.realpath(os.path.join(
        module_path,"output/local.bin"))

print(out_file_path)

with open(out_file_path, 'wb') as f:
    f.write(my_bytes)