471,350 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

finding dir of main .py file


Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:
myPythonApp.py
/resources
image1
image2
etc.

If i just do a call to os.getcwd() I get back the directory I was in when I
typed 'python myPythonApp.py' which could be any directory. What I want is
the directory that contains the file myPythonApp.py. Then I can use this
directory to construct the path to the resources directory.

(Actually, the app I'm writing has several subdirectories with stuff that it
needs such as plugins, configuration files, workspaces, etc.

Thanks for the help.

Ron
--
View this message in context: http://www.nabble.com/finding-dir-of...p14277145.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Dec 11 '07 #1
5 1977
On Dec 11, 10:08 am, "ron.longo" <long...@npt.nuwc.navy.milwrote:
Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:

myPythonApp.py
/resources
image1
image2
etc.
I just put the reference in my module. Don't hard code an absolute
path, use the environment tools.

app_path = os.getenv('HOME') + "/your_sub_dir"

resources_path = os.getenv('HOME') + "/your_sub_dir/resources"

If there's another way, someone else will jump in.

rd
Dec 11 '07 #2
Some usage of __file__ will always get what you want in various situations:

print __file__

print modulename.__file__

print os.getcwd() + "/" + __file__

Rick Dooling wrote:
On Dec 11, 10:08 am, "ron.longo" <long...@npt.nuwc.navy.milwrote:
>Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:

myPythonApp.py
/resources
image1
image2
etc.

I just put the reference in my module. Don't hard code an absolute
path, use the environment tools.

app_path = os.getenv('HOME') + "/your_sub_dir"

resources_path = os.getenv('HOME') + "/your_sub_dir/resources"

If there's another way, someone else will jump in.

rd

--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

Dec 11 '07 #3

Nope, maybe I'm not explaining myself well.

When I do os.getenv('HOME') I get back None.

According to the docs, 'HOME' is the user's home directory on some
platforms. Which is not what I want.

What I want is the directory in which an application's main .py file
resides. That is, when I type: python MyApp.py, I want to know in which
directory does MyApp.py reside?
Thanks,
Ron

Rick Dooling-2 wrote:
>
On Dec 11, 10:08 am, "ron.longo" <long...@npt.nuwc.navy.milwrote:
>Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:

myPythonApp.py
/resources
image1
image2
etc.

I just put the reference in my module. Don't hard code an absolute
path, use the environment tools.

app_path = os.getenv('HOME') + "/your_sub_dir"

resources_path = os.getenv('HOME') + "/your_sub_dir/resources"

If there's another way, someone else will jump in.

rd
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context: http://www.nabble.com/finding-dir-of...p14279627.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Dec 11 '07 #4
ron.longo wrote:
Nope, maybe I'm not explaining myself well.

When I do os.getenv('HOME') I get back None.

According to the docs, 'HOME' is the user's home directory on some
platforms. Which is not what I want.

What I want is the directory in which an application's main .py file
resides. That is, when I type: python MyApp.py, I want to know in which
directory does MyApp.py reside?
Shane is right.
>>print __file__
>>print modulename.__file__
Just call os.path.dirname() on __file__ to get the directory.
--
Dec 11 '07 #5
Shane Geiger <sg*****@ncee.netwrites:
Some usage of __file__ will always get what you want in various situations:

print __file__

print modulename.__file__

print os.getcwd() + "/" + __file__

Rick Dooling wrote:
>On Dec 11, 10:08 am, "ron.longo" <long...@npt.nuwc.navy.milwrote:
>>Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:

myPythonApp.py
/resources
image1
image2
etc.

The following two versions are working for me:

import os, sys

print os.path.abspath(os.path.dirname(sys.argv[0]))
print os.path.abspath(os.path.dirname(__file__))

Stefan.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 11 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Twan Kennis | last post: by
5 posts views Thread by Robert Manea | last post: by
6 posts views Thread by Tor Inge Rislaa | last post: by
13 posts views Thread by athiane | last post: by
12 posts views Thread by e271828 | last post: by
6 posts views Thread by begum | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.