UcanDoIt
UcanDoIt

Reputation:

split twice in the same expression?

Imagine I have the following:

inFile = "/adda/adas/sdas/hello.txt"

# that instruction give me hello.txt
Name = inFile.name.split("/") [-1]

# that one give me the name I want - just hello
Name1 = Name.split(".") [0]

Is there any chance to simplify that doing the same job in just one expression?

Upvotes: 6

Views: 2410

Answers (5)

Henrik Gustafsson
Henrik Gustafsson

Reputation: 54168

Answering the question in the topic rather than trying to analyze the example...

You really want to use Florians solution if you want to split paths, but if you promise not to use this for path parsing...

You can use re.split() to split using several separators by or:ing them with a '|', have a look at this:

import re
inFile = "/adda/adas/sdas/hello.txt"
print re.split('\.|/', inFile)[-2]

Upvotes: 2

Andrew Cox
Andrew Cox

Reputation: 10988

if it is always going to be a path like the above you can use os.path.split and os.path.splitext

The following example will print just the hello

from os.path import split, splitext
path = "/adda/adas/sdas/hello.txt"
print splitext(split(path)[1])[0]

For more info see https://docs.python.org/library/os.path.html

Upvotes: 1

Florian Bösch
Florian Bösch

Reputation: 27766

You can get what you want platform independently by using os.path.basename to get the last part of a path and then use os.path.splitext to get the filename without extension.

from os.path import basename, splitext

pathname = "/adda/adas/sdas/hello.txt"
name, extension = splitext(basename(pathname))
print name # --> "hello"

Using os.path.basename and os.path.splitext instead of str.split, or re.split is more proper (and therefore received more points then any other answer) because it does not break down on other platforms that use different path separators (you would be surprised how varried this can be).

It also carries most points because it answers your question for "one line" precisely and is aesthetically more pleasing then your example (even though that is debatable as are all questions of taste)

Upvotes: 20

Stein G. Strindhaug
Stein G. Strindhaug

Reputation: 5119

I'm pretty sure some Regex-Ninja*, would give you a more or less sane way to do that (or as I now see others have posted: ways to write two expressions on one line...)

But I'm wondering why you want to do split it with just one expression? For such a simple split, it's probably faster to do two than to create some advanced either-or logic. If you split twice it's safer too:

I guess you want to separate the path, the file name and the file extension, if you split on '/' first you know the filename should be in the last array index, then you can try to split just the last index to see if you can find the file extension or not. Then you don't need to care if ther is dots in the path names.

*(Any sane users of regular expressions, should not be offended. ;)

Upvotes: 0

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340221

>>> inFile = "/adda/adas/sdas/hello.txt"
>>> inFile.split('/')[-1]
'hello.txt'
>>> inFile.split('/')[-1].split('.')[0]
'hello'

Upvotes: 1

Related Questions