tags:

views:

570

answers:

6

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?

+1  A: 
>>> inFile = "/adda/adas/sdas/hello.txt"
>>> inFile.split('/')[-1]
'hello.txt'
>>> inFile.split('/')[-1].split('.')[0]
'hello'
Vinko Vrsalovic
+20  A: 

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)

Florian Bösch
This is the proper solution, but doesn't answer the question. Mine is the inverse :-) +1.
Vinko Vrsalovic
+1: proper solution to n00b question.
S.Lott
@Vinko Vrsalovicyours isn't the reverse, and it answers your question perfectly.
Florian Bösch
By "the inverse" I meant it answers the question but it's not the proper solution.
Vinko Vrsalovic
@Vinko Vrsalovic: Why is it not proper?
Florian Bösch
prettified a bit, feel free to rollback
J.F. Sebastian
Because to get filename from a path you should use the available functions for that purpose else your code becomes, at least, less portable.
Vinko Vrsalovic
@Vinko Vrsalovic: basename and splitext from os.path __are__ the proper available functions for that purpose... what other functions would you use for it?
Florian Bösch
That's what I was talking about from the first minute. Your solution is the proper one, and mine wasn't. I was talking about *my* solution being unproper because it does not use the available functions, not that yours was lacking in any way. Seems like there was a misunderstanding along the way here
Vinko Vrsalovic
A: 

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. ;)

Stein G. Strindhaug
+1  A: 

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 http://www.python.org/doc/2.5.2/lib/module-os.path.html

Andrew Cox
+1 for the manual cross-ref. That's always helpful. Give someone a fish ... teach someone how to fish ...
Jonathan Leffler
+2  A: 

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]
Henrik Gustafsson
A: 

The florian answer is perfect! I was just answering to stein... That asked me "But I'm wondering why you want to do split it with just one expression? "

Thank you guys for your help...

I just have a problem that even importing os I have an error that says splitext doesn't exist...

Double check:* that you are importing os.path, not os* that you are importing with the "from" construct to put splitext in your namespace.* That you are spelling it splitext not splittext
Oddthinking