在python中,如何检查文件名是以“.html”还是“_files”结尾? [英] In python, how can I check if a filename ends in '.html' or '_files'?
本文介绍了在python中,如何检查文件名是以“.html”还是“_files”结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,我怎么能检查文件名中 '的.html' 或 '_files' 结束?
In python, how can I check if a filename ends in '.html' or '_files'?
推荐答案
您可能想知道文件 name 是以这些字符串结尾,而不是文件istelf:
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
这篇关于在python中,如何检查文件名是以“.html”还是“_files”结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文