在python中,如何检查文件名是否以'.html'或'_files'结尾?

时间:2012-06-03 20:38:51

标签: python html

在python中,如何检查文件名是否以“.html”或“_files”结尾?

1 个答案:

答案 0 :(得分:9)

您可能想知道文件 name 是否以这些字符串结尾,而不是文件istelf:

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

要测试文件是否以其中一个字符串结尾,您可以执行以下操作:

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