splitting string

L

Laura McCord

I want to split a filename string such as Y:\folder\directory\file

it is stored like this in my python code:
fname = self.filename

How can I get rid of everything before the word 'file'?

I had tried something like this:
fname = self.filename
s = fname.split("\")
fname = s[-1]

Any suggestions?

Thanks
 
Z

Zunbeltz Izaola

Laura McCord said:
I want to split a filename string such as Y:\folder\directory\file

it is stored like this in my python code:
fname = self.filename

How can I get rid of everything before the word 'file'?

I had tried something like this:
fname = self.filename
s = fname.split("\")
fname = s[-1]

Any suggestions?

Try the os.path module

Zunbeltz

--
Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
dotes
Materia Kondentsatuaren Fisika Saila |
Zientzia eta Teknologia Fakultatea | Phone: 34946015326
Euskal Herriko Unibertsitatea |
PK 644 | Fax: 34 944648500
48080 Bilbo (SPAIN) |
 
M

Matthias Huening

I want to split a filename string such as Y:\folder\directory\file

it is stored like this in my python code:
fname = self.filename

How can I get rid of everything before the word 'file'?

Try this:
import os.path
x = os.path.split('Y:\\folder\\directory\\file')
x ('Y:\\folder\\directory', 'file')
x[1]
'file'

You'll need to escape the slashes.
Alternatively you could use r'...':
os.path.split(r'Y:\folder\directory\file')
or
os.path.split('Y:/folder/directory/file')

Matthias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top