最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Python crifan 5743浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.178 seconds, using 22.14MB memory