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?


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)

Need Your Help

Please explain me why 24,16, 8 were used in converting int to bytes?

java integer byte

The following code is to convert an int to Bytes array.

save image from fileupload

c# asp.net

I am using fileupload control to have image on the form and that that image has to be saved on some folder in hard disk say "E:\\asp_net".