How to refer to relative paths of resources when working with a code repository

We are working with a code repository which is deployed to both Windows and Linux – sometimes in different directories. How should one of the modules inside the project refer to one of the non-Python resources in the project (CSV files, etc.)? If we do something like: thefile = open(‘test.csv’) or: thefile = open(‘../somedirectory/test.csv’) It … Read more

How to get an absolute file path in Python

Given a path such as “mydir/myfile.txt”, how do I find the file’s absolute path relative to the current working directory in Python? E.g. on Windows, I might end up with: “C:/example/cwd/mydir/myfile.txt” 1Best Answer 11 >>> import os >>> os.path.abspath(“mydir/myfile.txt”) ‘C:/example/cwd/mydir/myfile.txt’ Also works if it is already an absolute path: >>> import os >>> os.path.abspath(“C:/example/cwd/mydir/myfile.txt”) ‘C:/example/cwd/mydir/myfile.txt’

Relative imports for the billionth time

I’ve been here: http://www.python.org/dev/peps/pep-0328/ http://docs.python.org/2/tutorial/modules.html#packages Python packages: relative imports python relative import example code does not work Relative imports in python 2.5 Relative imports in Python Python: Disabling relative import and plenty of URLs that I did not copy, some on SO, some on other sites, back when I thought I’d have the solution quickly. … Read more