python file suffix
python – How to get file extension correctly? – Stack Overflow
How to get the file extension from a filename in Python? – SysTutorials
Extracting extension from filename in Python – Stack Overflow
【总结】
最后用:
<code>def getFileSuffix(filename): </code>
“””
get file suffix from file name
no dot/period, no space/newline, makesure lower case
“xxx.mp3” -> “mp3”
“xxx.pdf” -> “pdf”
“xxx.mp3 ” -> “mp3”
“xxx.JPg” -> “jpg”
:param filename:
:return:
“””
fileSuffix = “”
if filename:
name, extension = os.path.splitext(filename)
fileSuffix = extension # .mp3
if fileSuffix:
# remove leading dot/period
fileSuffix = fileSuffix[1:] # mp3
if fileSuffix:
# remove ending newline or space
fileSuffix = fileSuffix.strip()
if fileSuffix:
# convert JPg to jpg
fileSuffix = fileSuffix.lower()
return fileSuffix
即可。
转载请注明:在路上 » 【已解决】Python中获取文件名后缀