473,769 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2160
On Dec 11, 10:08 am, "ron.longo" <long...@npt.nu wc.navy.milwrot e:
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.__fi le__

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

Rick Dooling wrote:
On Dec 11, 10:08 am, "ron.longo" <long...@npt.nu wc.navy.milwrot e:
>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.ne t | 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.nu wc.navy.milwrot e:
>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.__fi le__
Just call os.path.dirname () on __file__ to get the directory.
--
Dec 11 '07 #5
Shane Geiger <sg*****@ncee.n etwrites:
Some usage of __file__ will always get what you want in various situations:

print __file__

print modulename.__fi le__

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

Rick Dooling wrote:
>On Dec 11, 10:08 am, "ron.longo" <long...@npt.nu wc.navy.milwrot e:
>>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
subdirector y:

myPythonApp.py
/resources
image1
image2
etc.

The following two versions are working for me:

import os, sys

print os.path.abspath (os.path.dirnam e(sys.argv[0]))
print os.path.abspath (os.path.dirnam e(__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
15257
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 pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
15
7246
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 application (build in Visual Basic) using an ADO/OLE DB Provider on a IBM Client Access ODBC driver. On the Internet I found a whitepaper "SQL messages and codes" provided by IBM which describes a complete list of all SQLSTATEs, SQLCODEs and
5
5850
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. The purpose of the program is to serve as a facile 'strings' (Unix command) replacement and to be 100% ANSI C. Unfortunatelly it operates notedly slower than the original 'strings' from the fileutils package.
6
1262
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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click On Error GoTo errHandler
13
2013
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 in which the Function was found. What approach should i follow to tackle this problem ? Is there any option in the gcc compiler that prints out this information ?
12
3408
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, etc.). Pretty much all of the functionality is working except a feature that shows in which line of the HTML source a violation of the user-set rule occurs (e.g., where a LABEL element doesn't have a FOR attribute). There doesn't seem to be a...
0
1900
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 finding if the file exists, but i want to make it so that it tries to look for the computer, not the file. At the moment this is how i am finding and copying the file: If FileExists("\\oldpc\main share\Folder\file.txt") Then FileCopy "\\oldpc\main...
6
1980
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 System.Collections.Generic; using System.Text; using System.IO; namespace ConsoleApplication1
1
2206
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), inlined constructor implementation, and also declares a template function f. The file main.cpp contains some test code and also the default implementation for f(). The file spec.cpp contains some explicitly specialized stuff: ==== A.h ====
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.