user1020293
user1020293

Reputation: 205

How to delete end of a string in python

I have a string looks like this

urse/project/kb/use.js

from this string i need to get

urse/project/kb/

use.js the name will always change except .js.

Or delete the string till the first / from the end?

Upvotes: 0

Views: 120

Answers (2)

NPE
NPE

Reputation: 500773

Python has a dedicated module for dealing with file paths:

In [13]: os.path.dirname('urse/project/kb/use.js')
Out[13]: 'urse/project/kb'

Append a trailing os.sep as required (or, better yet, stick to using the os.path module for manipulating paths).

Upvotes: 7

applicative_functor
applicative_functor

Reputation: 4976

>>> s = 'urse/project/kb/use.js'
>>> s[:s.rfind('/') + 1]
'urse/project/kb/'

Upvotes: 2

Related Questions