A small script reads the list file located in the script directory:

 open(os.path.dirname(os.path.abspath(__file__))+'/list','w') as F 

I make a symbolic link:

 sudo ln -s /home/user/script/script.py /usr/local/bin/script 

And when I try to access the script via the link I get:

 FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/bin/list' 

How to refer to the file and still maintain portability?

    1 answer 1

    The working directory does not change. The current working directory for the process depends neither on the location of the link nor on the path to the source Python script. You can verify by calling os.getcwd() .

    A symlink surface problem — to find the path to a script in Python — can be solved by calling os.path.realpath() . Although a solution that uses __file__ may break down in the general case, instead of __file__ you can use the more versatile get_script_dir() function .

    A more regular solution is to use pkgutil.get_data() / pkg_resources to find the resources installed next to the Python code or the appdirs module to find the place where application / user data can be put. See Pythonw.exe does not work when working with external files.