473,407 Members | 2,306 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,407 software developers and data experts.

How to get full path to script?

kj

How can a script know its absolute path? (__file__ only gives the
path it was used to invoke the script.)

Basically, I'm looking for the Python equivalent of Perl's FindBin.

The point of all this is to make the scripts location the reference
point for the location of other files, as part of a self-contained
distribution.

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #1
8 2662

"kj" <so***@987jk.com.invalidwrote in message
news:g2**********@reader2.panix.com...
>
How can a script know its absolute path? (__file__ only gives the
path it was used to invoke the script.)

Basically, I'm looking for the Python equivalent of Perl's FindBin.

The point of all this is to make the scripts location the reference
point for the location of other files, as part of a self-contained
distribution.

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
import os
print os.path.abspath(__file__)

-Mark

Jun 27 '08 #2
kj
In <Bu******************************@comcast.com"Ma rk Tolonen" <M8********@mailinator.comwrites:
>import os
print os.path.abspath(__file__)
Great. Thanks!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #3
On Jun 8, 12:52*pm, kj <so...@987jk.com.invalidwrote:
In <Bu2dnUy5AvRQtdHVnZ2dnUVZ_tqdn...@comcast.com"Ma rk Tolonen" <M8R-yft....@mailinator.comwrites:
import os
print os.path.abspath(__file__)

Great. *Thanks!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Note that this doesn't quite work for symbolic links or compiled
scripts, depending on your requirements.
Jun 27 '08 #4
On Mon, Jun 9, 2008 at 12:37 AM, bukzor <wo**********@gmail.comwrote:
On Jun 8, 12:52 pm, kj <so...@987jk.com.invalidwrote:
>In <Bu2dnUy5AvRQtdHVnZ2dnUVZ_tqdn...@comcast.com"Ma rk Tolonen" <M8R-yft...@mailinator.comwrites:
>import os
print os.path.abspath(__file__)

Great. Thanks!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

Note that this doesn't quite work for symbolic links or compiled
scripts, depending on your requirements.
--
http://mail.python.org/mailman/listinfo/python-list
For my compiled scripts, I usually use this variation:

path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0])))

It's always worked for me.

Mike
Jun 27 '08 #5
kj
In <ma*************************************@python.or g"Mike Driscoll" <ky******@gmail.comwrites:
>For my compiled scripts, I usually use this variation:
>path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0])))
Thanks. But why the os.path.join()? (BTW, I did read the docs
before posting, but they make no sense to me; they say that
os.path.join joins "one or more path components intelligently",
but what does it mean to join *one* component?)

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #6
On Mon, Jun 9, 2008 at 11:07 AM, kj <so***@987jk.com.invalidwrote:
In <ma*************************************@python.or g"Mike Driscoll" <ky******@gmail.comwrites:
>>For my compiled scripts, I usually use this variation:
>>path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0])))

Thanks. But why the os.path.join()? (BTW, I did read the docs
before posting, but they make no sense to me; they say that
os.path.join joins "one or more path components intelligently",
but what does it mean to join *one* component?)

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list
The idea of the join method is to create the path in an OS agnostic
fashion. Linux uses forward slashes and Windows uses backward slashes
to join the parts. The join method does this for you so you don't have
to.

I think in this case, if I had my program installed to

C:\Program Files\MyProgram

It would put the slashes in correctly for Windows. However, there are
ways to get the default program directory in Linux and then have the
os.path.join create the path correctly there too. That's the idea
anyway. Hopefully that isn't more confusing than what you read.

Mike
Jun 27 '08 #7
Mike Driscoll <ky******@gmail.comat Montag 09 Juni 2008 18:20:
On Mon, Jun 9, 2008 at 11:07 AM, kj <so***@987jk.com.invalidwrote:
>In <ma*************************************@python.or g"Mike Driscoll"
<ky******@gmail.comwrites:
>>>For my compiled scripts, I usually use this variation:
>>>path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0])))

Thanks. But why the os.path.join()? (BTW, I did read the docs
before posting, but they make no sense to me; they say that
os.path.join joins "one or more path components intelligently",
but what does it mean to join *one* component?)

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list

The idea of the join method is to create the path in an OS agnostic
fashion. Linux uses forward slashes and Windows uses backward slashes
to join the parts. The join method does this for you so you don't have
to.
I guess, you didn't get his point. He seems to be aware that os.path.join
creates a path from _multiple_ strings by joining them with the correct
separator used by the underlying platform.

But he was asking why one would invoke os.path.join on a _single_ string, as
you did in your example. I'm wondering about this, too. It doesn't make
sense to me. os.path.join doesn't convert existing separators to the
platform-specific ones. And even if it would, sys.argv[0] already contains
a correct path, so there is nothing that needs conversion. So why use it
with a _single_ argument? I'd appreciate an example, illustrating the use
of this ;)
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jun 27 '08 #8
On Mon, Jun 9, 2008 at 12:42 PM, Sebastian lunar Wiesner
<ba***********@gmx.netwrote:
Mike Driscoll <ky******@gmail.comat Montag 09 Juni 2008 18:20:
>On Mon, Jun 9, 2008 at 11:07 AM, kj <so***@987jk.com.invalidwrote:
>>In <ma*************************************@python.or g"Mike Driscoll"
<ky******@gmail.comwrites:

For my compiled scripts, I usually use this variation:

path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0])))

Thanks. But why the os.path.join()? (BTW, I did read the docs
before posting, but they make no sense to me; they say that
os.path.join joins "one or more path components intelligently",
but what does it mean to join *one* component?)

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list

The idea of the join method is to create the path in an OS agnostic
fashion. Linux uses forward slashes and Windows uses backward slashes
to join the parts. The join method does this for you so you don't have
to.

I guess, you didn't get his point. He seems to be aware that os.path.join
creates a path from _multiple_ strings by joining them with the correct
separator used by the underlying platform.

But he was asking why one would invoke os.path.join on a _single_ string, as
you did in your example. I'm wondering about this, too. It doesn't make
sense to me. os.path.join doesn't convert existing separators to the
platform-specific ones. And even if it would, sys.argv[0] already contains
a correct path, so there is nothing that needs conversion. So why use it
with a _single_ argument? I'd appreciate an example, illustrating the use
of this ;)
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list
Okay, basically the answer is that I'm kind of stupid. Months ago, the
users on the wxPython group were discussing this issue and one of them
posted that snippet of code to show how they worked around the issue.
I thought I'd try it and it worked great, although I couldn't really
follow what was happening at the time.

Looking at it now, there doesn't appear to be any reason for the
os.path.join part. I tried running one of my simple scripts with and
without it and they return the same string.

I apologize for propagating erroneous code.

Mike
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Don | last post by:
How do I retrieve the full path (C:\.....filename) of a file uploaded to a php script on the server. I think $_FILES will only provide 'name', which is only the filename itself. Thanks, Don ...
1
by: Yang | last post by:
I want to list all images from a url. here is my code snippet which finds an image from a url: $url = "http://asdf.com/"; $text = @implode("", file($url)); while (eregi("*(src)*=*(+)",...
7
by: Benjamin Han | last post by:
I know I can do this by get sys.argv, tell if it's a full path, and if not, somehow join the relative path with getcwd(). Just wondering if there's a simpler way to do this. Thanks!
2
by: Nathan Sokalski | last post by:
I have a rollover script that I plan to use for many of my pages. When I preload the images, the directories must be relevant to the location of the HTML file that uses the script, which will not...
15
by: (Pete Cresswell) | last post by:
I've got a .BAT file that I use for executing various MS Access apps that I wrote way back in the days of 2.0. It's evolved over time, but it still contains a number of possible paths to...
3
by: PHaroZ | last post by:
Hi, I want to retrieve the complete full path to the directory of my current page but i don't find how to do that. For example i want : D:\myWebSite\firstDotNetWebApp\dir1\ I tried...
6
by: MattB | last post by:
Is there something in the .Net libraries that will let me retrieve (or get enough information to recreate) the full URL of a page in my application? Sort of like the reverse Server.MapPath() that...
7
by: gmax2006 | last post by:
Hi, I use RedHat linux. How can I find where exactly the current python script is running? I use this code: #test.py import os,sys
11
by: rh00667 | last post by:
hi all, i'm confused now. how i can get the full path of an application? if myapp is in a directory which belongs to PATH, argv gives me the first token of cmd line, and not the real path of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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...

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.