我有很长的文件路径,例如:
D:%5CMedia%5CMusic%20Videos%5CAlexis%20Jordan%20-%20Good%20Girl%2Emkv
从那里获取文件名的最佳方法是什么,所以我最终得到:
Alexis Jordan - Good Girl
从那里我想将艺术家和标题分成单独的部分,但我可以做到:)
首先,您需要解码 URL 编码,urllib.unquote()
然后使用os.path
模块拆分文件名和扩展名:
import os
import urllib
path = urllib.unquote(path)
filename = os.path.splitext(os.path.basename(path))[0]
whereos.path.basename()
删除目录路径,并os.path.splitext()
为您提供文件名和扩展名元组。
然后,这将为您提供文件名:
>>> 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))