[Tutor] getting filen basename without extension
Rolando Pereira
finalyugi at sapo.pt
Thu Jan 10 11:40:17 CET 2008
Timmie wrote:
> Hello,
> I would like to get the name of a file without it's extension/suffix.
>
> What is the easiest and fastes way to get the basename
> of a file wihout extension?
>
> What I found is this:
> import os
> myfile_name_with_path = 'path/to/my/testfile.txt'
> basename = os.path.basename(myfile_with_path)
> filename = os.path.splitext(basename)
> myfile_name_without_suffix = filename[0]
>
> Can this be done with less code?
>
> Thanks and kind regards,
> Timmie
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
I did something like this:
import os
path = 'path/to/file.ext'
filename = path.split(".")[0].split("/")[-1]
print filename
>>> file
The only problem I see is if the file has some "." character besides the
one before the extention.
--
_
ASCII ribbon campaign ( )
- against HTML email X
& vCards / \
More information about the Tutor
mailing list