why o/p is different ???

Lie Lie.1296 at gmail.com
Fri Jan 16 14:18:10 EST 2009


On Jan 15, 1:34 pm, asit <lipu... at gmail.com> wrote:
> I recently faced a peculiar o/p.
>
> My objective is to remove the command name(my script name) from
> sys.argv[0].
> I coded like this
>
> import urllib
> import sys
>
> print "\n\n\t\tlipun4u[at]gmail[dot]com"
> print "\t\t------------------------"
>
> apppath = sys.argv[0].split("/")
> appname = apppath[len(apppath)-1]
> print appname
>
> if len(sys.argv) not in [2,3]:
>     print "Usage : " + appname + " <url> [options]"
>     print "e.g. : " + appname + "www.google.com--verbose"
>     print "\n\t[option]"
>     print "\t\t--verbose/-V for verbose output"
>     print "\t\t-r for recursive scan"
>     sys.exit(1)
>
> site = appname.replace("http://","").rsplit("/",1)[0]
> site = "http://" + site.lower()
> print site
>
> It showed the required o/p in the IDLE
> here is the o/p
>
>                 lipun4u[at]gmail[dot]com
>                 ------------------------
> linkscan.py
> Usage : linkscan.py <url> [options]
> e.g. : linkscan.pywww.google.com--verbose
>
>         [option]
>                 --verbose/-V for verbose output
>                 -r for recursive scan
>
> Traceback (most recent call last):
>   File "I:/Python26/linkscan.py", line 18, in <module>
>     sys.exit(1)
> SystemExit: 1
>
> But in command prompt the o/p is still faulty
>
>                 lipun4u[at]gmail[dot]com
>                 ------------------------
> I:\Python26\linkscan.py
> Usage : I:\Python26\linkscan.py <url> [options]
> e.g. : I:\Python26\linkscan.pywww.google.com--verbose
>
>         [option]
>                 --verbose/-V for verbose output
>                 -r for recursive scan
>
> I:\Python26>
>
> regards
> asit dhal

Of course the pragmatic approach is just to include

appname = 'linkscan.py'

at the top of the document.

I agree apppath might change (because you copied the program to
different places, different bin directory on different machine), but
there is no reason linkscan.py should change. And if you have to
change the invoking name, sometimes it might be more useful for users
to know the REAL name of the program (probably the user friendly
version, e.g. "Link Scanner" or "Link Scanner 1.0"), instead of the
name of the script it is run from (without the apppath). It is trivial
for human user to guess where the script is located and what the
appname is from the full apppath (path+name)



More information about the Python-list mailing list