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 :)
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'
from urllib2 import unquote
from os.path import basename
p = 'D:%5CMedia%5CMusic%20Videos%5CAlexis%20Jordan%20-%20Good%20Girl%2Emkv'
fname = basename(unquote(p))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.