pythonstringpathfastq

How to get a base file name from a path without multiple extensions


I have the following path:

f_file = /home/reads_dataset_1/E2_ER/E2_ER_exp1_L1.fastq.gz

And I'd like to get only the base file name without the 2 extensions (that is, without .fastq.gz):

E2_ER_exp1_L1

Tried:

sample_name = os.path.splitext(f_file)[0]

But I got the whole name of the path without the last extension.


Solution

  • may be funny and dirty, but works :)

    sample_name = os.path.splitext(os.path.splitext(os.path.basename(f_file))[0])[0]
    

    also can use shorter, nicer version:

    sample_name = os.path.basename(f_file).split('.')[0]