How to open a txt file from the same folder as my module (w/out changing the working dir)

Sergio Correia sergio.correia at
Thu Apr 5 01:03:01 CEST 2007

I have  a program in 'C:\Python25\Lib\site-packages\spam\'

Importing and everything works fine:
>>> from spam import spam

But the program calls a file located on the same folder (that is:

How do i do that?

>>> spam.eggs()

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    datita = spam.eggs()
  File "C:\Python25\lib\site-packages\spam\", line 149, in JustDoIt
    config = open("configuration.txt", "rb").read().split('\r\n')
IOError: [Errno 2] No such file or directory: 'configuration.txt'

My last resort is to hard code the path for that file, but it's ugly,
and I want to know if I'm missing something. Am I?

Thanks a lot,

More information about the Python-list mailing list