nobodyAskedYouPatrice
nobodyAskedYouPatrice

Reputation: 131

How to use Regular Expressions to select a single part of a file path

I have a file path like this:

"C:/Users/myname/Documents/KF0_IFN_HLA_11.csv"

How could I use regular expressions to just get the "KF0_IFN_HLA_11.csv" part?

I'm a beginner to Python and I'm just looking for some tips about how to figure out the problem above.

Upvotes: 0

Views: 402

Answers (2)

cs95
cs95

Reputation: 402333

Option 1 (not recommended):

Using re.search (import re first):

In [1099]: re.search(r'.*/(.*)$', text).group(1)
Out[1099]: 'KF0_IFN_HLA_11.csv'

The pattern is

r'.*/(.*)$'

It extracts the bit after the last forward slash.


Option 2 (recommended):

Using os.path.split or os.path.basename (import os first):

In [1100]: os.path.split(text)[1]
Out[1100]: 'KF0_IFN_HLA_11.csv'

In [1101]: os.path.basename(text)
Out[1101]: 'KF0_IFN_HLA_11.csv'

os.path.split splits the path into the head and tail, here you just extract the tail, because that's what you need.

os.path.basename returns the tail automatically.

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117856

I wouldn't use regex for this, rather the os.path module is much more appropriate. Using os.path.basename will extract the file without the full path.

>>> import os
>>> p = r"C:/Users/myname/Documents/KF0_IFN_HLA_11.csv"
>>> os.path.basename(p)
'KF0_IFN_HLA_11.csv'

Upvotes: 3

Related Questions