2

I have long paths to files like:

D:%5CMedia%5CMusic%20Videos%5CAlexis%20Jordan%20-%20Good%20Girl%2Emkv

What is the best way to get just the file name from there, so I end up with:

Alexis Jordan - Good Girl

From there I want to cut the Artist and Title into separate parts, but I can manage that :)

2
  • Do you have some set of keywords with you like 'Media', 'Music', 'Videos', which will remain the same? Commented Jan 24, 2013 at 21:52
  • @Vikram - Unfortunately not. I was thinking about cutting it off from the last \ and then cutting the last 4 characters, but I'm not sure how to do that. Also I need to convert the %20 to spaces. Commented Jan 24, 2013 at 21:53

2 Answers 2

7

First you need to decode the URL encoding with urllib.unquote() then use the os.path module to split out the filename and extension:

import os
import urllib

path = urllib.unquote(path)
filename = os.path.splitext(os.path.basename(path))[0]

where os.path.basename() removes the directory path, and os.path.splitext() gives you a filename and extension tuple.

This then gives you the filename:

>>> import os
>>> import urllib
>>> path = 'D:%5CMedia%5CMusic%20Videos%5CAlexis%20Jordan%20-%20Good%20Girl%2Emkv'
>>> path = urllib.unquote(path)
>>> path
'D:\\Media\\Music Videos\\Alexis Jordan - Good Girl.mkv'
>>> filename = os.path.splitext(os.path.basename(path))[0]
>>> filename
'Alexis Jordan - Good Girl'
Sign up to request clarification or add additional context in comments.

Comments

3
from urllib2 import unquote
from os.path import basename

p = 'D:%5CMedia%5CMusic%20Videos%5CAlexis%20Jordan%20-%20Good%20Girl%2Emkv'
fname = basename(unquote(p))

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.