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

Simple way to get the full path of a running script?

I know I can do this by get sys.argv[0], 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!
Jul 18 '05 #1
7 2772
Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys .argv[0])[0])
On Sat, 27 Dec 2003, Benjamin Han wrote:
I know I can do this by get sys.argv[0], 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!

Jul 18 '05 #2
Should be:

pathToScript=\
os.path.normpath(os.path.join(os.getcwd(),os.path. split(sys.argv[0])[0]))

On Sat, 27 Dec 2003, Benjamin Han wrote:
Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys .argv[0])[0])
On Sat, 27 Dec 2003, Benjamin Han wrote:
I know I can do this by get sys.argv[0], 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!

Jul 18 '05 #3
On Sat, 2003-12-27 at 18:02, Benjamin Han wrote:
Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys .argv[0])[0])


Or:

#!/usr/bin/env python

import sys
import os

me = sys.argv[0]
print "This is how you invoked me: %s" % (me,)
print "This is the absolute path: %s" % (os.path.abspath(me),)

Usage:

mark@dev /var/tmp/buffer
$ python ../junk.py
This is how you invoked me: ../junk.py
This is the absolute path: /var/tmp/junk.py

Cheers,

// m
Jul 18 '05 #4
On Sat, 27 Dec 2003, Mark McEahern wrote:
On Sat, 2003-12-27 at 18:02, Benjamin Han wrote:
Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys .argv[0])[0])


me = sys.argv[0]
print "This is the absolute path: %s" % (os.path.abspath(me),)


Thank you - this is even better!
Jul 18 '05 #5
> #!/usr/bin/env python

import sys
import os

me = sys.argv[0]
print "This is how you invoked me: %s" % (me,)
print "This is the absolute path: %s" % (os.path.abspath(me),)

Usage:

mark@dev /var/tmp/buffer
$ python ../junk.py
This is how you invoked me: ../junk.py
This is the absolute path: /var/tmp/junk.py

Is this going to work well cross platform? How about from IDLE?

I was using sys.path[0] which I thought was working well, but
apparently fails when the script is run from IDLE on windows.

Jul 18 '05 #6
i normally use the following:

import inspect
print inspect.getsourcefile( lambda:None )

the lambda function could be any object; the getsourcefile returns the
path to the file where that object was defined.

_wolf
Jul 18 '05 #7
Hi,

Wolfgang Lipp schrieb:
import inspect
print inspect.getsourcefile( lambda:None )


Nice idea!

Enhancement: when using inspect.getfile() the snippet will work even for
module without sourcefile, too.

--
Regards
Hartmut Goebel

| Hartmut Goebel | We build the crazy compilers |
| h.******@crazy-compilers.com | Compiler Manufacturer |

Jul 18 '05 #8

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

Similar topics

1
by: Preston Crawford | last post by:
I'm looking to quickly get a photo album online. Very simple, thumbnails, a few pages, maybe a description, but hopefully a small script that's easy to edit and work into my existing site. I know...
3
by: Peter Schwalm | last post by:
I'd like to modify the python search path depending on the source directory of the script being started. The reason is: I use a version control system, and the python scripts and modules are...
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...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
2
by: Sridhar | last post by:
Hi, I have a web form where it has a <input type=file id=file1> control. I have an Upload button to upload the file. WHen I click on browse and select one file, it is showing the full file path...
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
4
by: somank.sharma | last post by:
I am running an exe created in C. I need the full path (absolute path) of this exe. In the first argument of main, I am getting the exe name. How can I get the full path for this exe.
7
by: misceverything | last post by:
Hello, I would like to write a script that would enumerate all running processes and return the full path to the EXE of each running process. However, I can't seem to find any good info on how...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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.