469,271 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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

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.

Thanks,
Peng

#!/usr/bin/env python

import os

cmd = "./some_command";
print cmd
os.system(cmd);

Oct 24 '06 #1
5 1598
At Tuesday 24/10/2006 20:39, Pe*******@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__)
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ”gratis!
”Abrķ tu cuenta ya! - http://correo.yahoo.com.ar
Oct 24 '06 #2


On Oct 24, 7:01 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
At Tuesday 24/10/2006 20:39, PengYu...@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

Oct 25 '06 #3
import os
print os.path.dirname(os.path.abspath(__file__))

--
Jerry

Oct 25 '06 #4
Pe*******@gmail.com wrote:
>
On Oct 24, 7:01 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
>>At Tuesday 24/10/2006 20:39, PengYu...@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? ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 25 '06 #5
On Wed, 2006-10-25 at 02:49 +0100, Steve Holden wrote:
Pe*******@gmail.com wrote:

On Oct 24, 7:01 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
>At Tuesday 24/10/2006 20:39, PengYu...@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.

Oct 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Greg Yasko | last post: by
1 post views Thread by Dan Jones | last post: by
2 posts views Thread by graphicsxp | last post: by
6 posts views Thread by Mudcat | last post: by
1 post views Thread by Calvin Spealman | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.