4

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.

3 Answers 3

6

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]

Sign up to request clarification or add additional context in comments.

3 Comments

could just split at . etc, but maybe this is easier to adopt to your current code :)
That gives me the whole path without the 2 extensions, and I want to get only the file name: E2_ER_exp1_L1. Is there a short way instead of using split?
os.path.basename gives you only file name
2

I know this is old, but since pathlib was not mentioned:

Use pathlib.Path

from pathlib import Path f_file = "/home/reads_dataset_1/E2_ER/E2_ER_exp1_L1.fastq.gz" f_path = Path(f_file) sstem = None # Repeatedly get the stem until no change while sstem != f_path.stem: sstem = f_path.stem f_path = Path(sstem) print(f_path.stem) 

You'll get:

E2_ER_exp1_L1 

Or if you know exactly that there will be two suffixes:

from pathlib import Path f_file = "/home/reads_dataset_1/E2_ER/E2_ER_exp1_L1.fastq.gz" f_path = Path(f_file) stem1 = f_path.stem stem2 = Path(stem1).stem print(stem2) 

Comments

0

Use os.path.basename and removesuffix:

import os sample_name = os.path.basename('/home/reads_dataset_1/E2_ER/E2_ER_exp1_L1.fastq.gz').removesuffix('.fastq.gz') # sample_name = 'E2_ER_exp1_L1' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.