In python, how can I check if a filename ends in '.html' or '_files'?

后端 未结 1 805
日久生厌
日久生厌 2021-01-29 07:05

In python, how can I check if a filename ends in \'.html\' or \'_files\'?

相关标签:
1条回答
  • 2021-01-29 07:44

    You probably want to know if a file name ends in these strings, not the file istelf:

    if file_name.endswith((".html", "_files")):
        # whatever
    

    To test whether a file ends in one of these strings, you can do this:

    with open(file_name) as f:
        f.seek(-6, 2)           # only read the last 6 characters of the file
        if f.read().endswith((".html", "_files")):
            # whatever
    
    0 讨论(0)
提交回复
热议问题