How to find out the directory that the py file is in?

John McMonagle jmcmonagle at velseis.com.au
Wed Oct 25 04:29:46 CEST 2006


On Wed, 2006-10-25 at 02:49 +0100, Steve Holden wrote:
> PengYu.UT at gmail.com wrote:
> > 
> > On Oct 24, 7:01 pm, Gabriel Genellina <gagsl... at yahoo.com.ar> wrote:
> > 
> >>At Tuesday 24/10/2006 20:39, PengYu... at gmail.com wrote:
> >>
> >>
> >>>I have the following python script and some_command in the same
> >>>directory. I have to call the python script from that directory.
> >>>Otherwise, some_command won't be found. I'm wondering how to make the
> >>>following script working no matter what the working directory is.print
> > 
> > 
> >>os.path.abspath(__file__)
> > 
> > 
> > How to get the absolute dir name which the file is in? Is there any
> > function can be called easily?
> > 
> > Thanks,
> > Peng
> > 
> 
> That would be
> 
>    os.path.split(os.path.abspath(__file__))[0]
> 
> You *are* planning to read the documentation at some stage, right? ;-)
> 

More simple:

os.path.dirname(os.path.abspath(__file__))



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the Python-list mailing list