[Tutor] getting filen basename without extension

Tiger12506 keridee at jayco.net
Fri Jan 11 22:36:11 CET 2008


I would always use the os functions to find the basename, but
You could exploit the fact that Windows considers the extension to be 
whatever is after the last "." character.

fn = fn[:fn.rfind(".")]

>> > 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]
>>
>> The last two can be easily combined as:
>>
>> > myfile_name_without_suffix = os.path.splitext(basename)[0]
>>
>> But otherwise you are using the "correct" mechanism if
>> you want a reliable result.
> Thanks for this confirmation.
>
> I am happy to have made some tiny progress in my python learings...
>
> Kind regards,
> Timmie
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list