A Simple question about a slice of a string.

Jeff Collins collins at seal.aero.org
Mon Jun 5 16:00:05 EDT 2000



Jay R. Wren writes:
 > I'm attempting to accomplish grabbing the last 6 characters of sys.argv[0]
 > so that I can compare it to another string.
 > If 'myprog' is called as /usr/local/bin/myprog.  It could be called from any
 > path, as long as the 'myprog' portion is there, certain things will
 > execute.(this is used as a symlink)
 > 
 > myprogname = sys.argv[0][ len(sys.argv[0])-10:len(sys.argv[0])

The os.path module provides some useful services:

>>> import os
>>> os.path.split("/usr/local/bin/myprog")
('/usr/local/bin', 'myprog')
>>> head, tail = os.path.split("/usr/local/bin/myprog")
>>> tail
'myprog'
>>> head
'/usr/local/bin'


Jeff




More information about the Python-list mailing list