getting command line in python

Chris Rebert clp2 at rebertia.com
Tue Nov 8 17:32:30 EST 2011


On Tue, Nov 8, 2011 at 2:19 PM, MrSmile <no at mail.de> wrote:
> Hi people!
> I am looking for a way to get the command line in the script.
>
> Let us say I am in the folder "/tmp" okay!
>
> now from /tmp I execute in the console this:
> python /home/tamer/MyApp/MyScript.py
>
> in the app itself, I want to grep the path and the scriptname itself,
> like: /home/tamer/MyApp is being somewhere available.

Under your example conditions:
sys.argv[0] will be "/home/tamer/MyApp/MyScript.py". (A)
os.getcwd() will return "/tmp". (B)

The `os.path` module is also very relevant:
http://docs.python.org/library/os.path.html

Cheers,
Chris
--
http://rebertia.com

(A): http://docs.python.org/library/sys.html#sys.argv
(B): http://docs.python.org/library/os.html#os.getcwd



More information about the Python-list mailing list