最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Python中获取文件名后缀

Python crifan 498浏览 0评论

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中获取文件名后缀

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
59 queries in 0.105 seconds, using 18.92MB memory