scriptname = scriptfile.split("/")[-1].split(".")[0]
what does that above does?
any example for that?
DougieLawson wrote:http://docs.python.org/2/library/stdtyp ... #str.split
Looks like it will split /dir/subdir/subsubdir/file.name
into scriptname[0] == "dir", scriptname[1] == "subdir", scriptname[2] == "subsubdir" and scriptname[3] = "file.name"
Why not try it.lilzz wrote:DougieLawson wrote:http://docs.python.org/2/library/stdtyp ... #str.split
Looks like it will split /dir/subdir/subsubdir/file.name
into scriptname[0] == "dir", scriptname[1] == "subdir", scriptname[2] == "subsubdir" and scriptname[3] = "file.name"
what about those [-1] and [0] things? I am not too clear on that.
Code: Select all
pi@pi ~ $ python
Python 2.7.6 (default, Feb 28 2014, 07:43:05)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> script = "/home/pi/foo/bar.file"
>>> scriptname = script.split("/")[-1].split(".")[0]
>>> print scriptname
bar
>>> print scriptname[0]
b
>>> print scriptname[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> print scriptname[1]
a
>>>