473,241 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 2121
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
15
by: Twan Kennis | last post by:
Hi, I have a DB2 database on the IBM iSeries platform, on which I created several Stored Procedures with the SQLCODE as a return-parameter. These Stored Procedures are called from a Windows...
5
by: Robert Manea | last post by:
Hello everyone, I wrote, simply as an exercise, a small piece of code to find 'strings' (defined as an amount of at least 3 ASCII characters followed by a non ASCII character) in binary files. ...
6
by: Tor Inge Rislaa | last post by:
Finding current Procedure I want to write errors to a log-file, where the error log contain the description of where the error occurred and what kind of error it was. Private Sub...
13
by: athiane | last post by:
I want a way to parse out all function names that appear in a couple of C files. When the parsing logic finds a function name in a file, it should print out the Function name, line number and file...
12
by: e271828 | last post by:
Hi, I'm helping to work a developer tool that verifies a given HTML element has a given attribute (e.g., that all LABEL elements have a FOR attribute, all INPUT elements have an ID attribute,...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
6
by: begum | last post by:
HI everybody; I have problem about selecting files in my main file. I have to find the files whose ending INF. What Can I do?Can anybody help me? using System; using...
1
by: jason.cipriani | last post by:
Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter),...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.