Methods to avoid hard-coding file paths in Python

Viewed 6976

Working with scientific data, specifically climate data, I am constantly hard-coding paths to data directories in my Python code. Even if I were to write the most extensible code in the world, the hard-coded file paths prevent it from ever being truly portable. I also feel like having information about the file system of your machine coded in your programs could be security issue.

What solutions are out there for handling the configuration of paths in Python to avoid having to code them out explicitly?

4 Answers

First off its always good practise to add a main function to go with each class to test that class or functions in the file. Along with this you determine the current working directory. This becomes incredibly important when running python from a cron job or from a directory that is not the current working directory. No JSON files or environment variables are then needed and you will obtain interoperation across Mac, RHEL and Debian distributions.

This is how you do it, and it will work on windows also if you use '\' instead of '/' (if that is even necessary, in your case).

if "__main__" == __name__:
    workingDirectory  = os.path.realpath(sys.argv[0])

As you can see when you run your command, the working directory is calculated if you provide a full path or relative path, meaning it will work in a cron job automatically.

After that if you want to work with data that is stored in the current directory use:

fileName = os.path.join( workingDirectory, './sub-folder-of-current-directory/filename.csv' )
fp       = open( fileName,'r')

or in the case of the above working directory (parallel to your project directory):

fileName = os.path.join( workingDirectory, '../folder-at-same-level-as-my-project/filename.csv' )
fp       = open( fileName,'r')
Related