Use os.path.basename : >>> os.path.basename('/foo/bar/baz.py') 'baz.py'
It will have the expected behavior on each system. You could also use os.sep instead of '/' in your split method call, but os.path.basename is more elegant. On Thu, Jan 15, 2009 at 7:34 AM, asit <lipu...@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 > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list