lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Snippet of Parsing

Mon Mar 10, 2014 8:46 pm

scriptname = scriptfile.split("/")[-1].split(".")[0]

what does that above does?
any example for that?

User avatar
croston
Posts: 707
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Snippet of Parsing

Mon Mar 10, 2014 8:49 pm

It strips off the pathname and file extension from a filename to give the script name.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Snippet of Parsing

Mon Mar 10, 2014 8:51 pm

http://docs.python.org/2/library/stdtyp ... #str.split

Looks like it will split /dir/subdir/subsubdir/file.name
into scriptname== "file"
dropping all of the other parts of the filename and directory names.
Last edited by DougieLawson on Mon Mar 10, 2014 8:54 pm, edited 1 time in total.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: Snippet of Parsing

Mon Mar 10, 2014 8:53 pm

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.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Snippet of Parsing

Mon Mar 10, 2014 8:55 pm

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.
Why not try it.

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
>>>
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
croston
Posts: 707
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Snippet of Parsing

Mon Mar 10, 2014 8:57 pm

[-1] is the last item in a list, [0] is the first item in a list.

Return to “Python”