How to refer to data files without hardcoding paths?

Ben Finney ben+python at
Sun Sep 6 03:49:26 CEST 2009

Matthew Wilson <matt at> writes:

> Today I realized that I'm hardcoding paths in my app. They are
> relative paths based on os.getcwd(), but at some point, I'll be
> running scripts that use this code, these open(...) calls will fail.

The conventional solution to this is:

* Read configuration settings, whether directory paths or anything else,
  from a configuration file of declarative options.

* Have the program read that configuration file from one location (or a
  small number of locations), and make those locations well-known in the
  documentation of the program.

Python's standard library has the ‘configparser’ module, which is one
possible implementation of this.

 \          “I used to be an airline pilot. I got fired because I kept |
  `\       locking the keys in the plane. They caught me on an 80 foot |
_o__)                    stepladder with a coathanger.” —Steven Wright |
Ben Finney

More information about the Python-list mailing list