[Tutor] reference to directory a module is located at

Strax-Haber, Matthew (LARC-D320) matthew.strax-haber at nasa.gov
Thu Jun 18 21:43:08 CEST 2009


Whoops. I should read more carefully. I thought he said the path to the module itself. Yes, os.path.dirname(__file__) works.
If you want an absolute path (rather than a relative path), use:
os.path.abspath(os.path.dirname(__file__))
I would find this more useful if the path will be passed around.
--
~ Matthew Strax-Haber
National Aeronautics and Space Administration
Langley Research Center (LaRC)
Co-op, Safety-Critical Avionics Systems Branch
Student, Northeastern University
W: 757-864-7378; C: 561-704-0029
Matthew.Strax-Haber at nasa.gov


________________________________
From: Dave Angel <davea at ieee.org>
Date: Thu, 18 Jun 2009 11:48:40 -0500
To: Python Tutor <tutor at python.org>
Subject: Re: [Tutor] reference to directory a module is located at

bob gailer wrote:

> Elisha Rosensweig wrote:
>> > Hi,
>> >
>> > How can I determine the directory in which a module is located, from
>> > within that module?
>>
> sys.modules[__name__].__file__
> -- Bob Gailer
Or more simply,
    __file__

But the OP wanted the directory, which can be extracted with method dirname:

import os
print __file__
print os.path.dirname(__file__)


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090618/118afcf7/attachment-0001.htm>


More information about the Tutor mailing list