Programming Best Practices

Use Relative Paths

For code portability and ease-of-use, we recommend always using relative paths instead of hard paths … for example:

If your code is in your homedir (/home/dept/username/mycode.py) and you’re opening a data file (/home/dept/username/data/datafile1), you can remove the ‘/home/dept/username/’ from your code, and simply use ‘data/datafile1’ (note the lack of leading slash).

If your code is in a subdir (/home/dept/username/code/mycode.py) and you’re opening the same data file (/home/dept/username/data/datafile1), the best practice is to not run the code from within the code directory, but from within your home directory, with code/mycode.py, referencing the data as above with data/datafile1.

Even better: you can move the code into a project directory, and run the job from there, specifying the subdirectory for the code when referencing code, and the data sub-directory when referencing data:

Complete paths for your project:

If you run the code from within the ‘project’ directory, you’ll be running ‘code/mycode.py‘, and in the code you’ll reference data/datafile1, and writing output to out/outfile.txt

This way your code is portable, and you won’t have to modify paths if you run it in Wharton’s HPC environment, your desktop, the Amazon AWS cloud, or pass it to a collaborator, student, or post it on your blog.