In python, how can I check if a filename ends in ‘.html’ or ‘_files’?
Advertisement
Answer
You probably want to know if a file name ends in these strings, not the file istelf:
JavaScript
x
3
1
if file_name.endswith((".html", "_files")):
2
# whatever
3
To test whether a file ends in one of these strings, you can do this:
JavaScript
1
5
1
with open(file_name) as f:
2
f.seek(-6, 2) # only read the last 6 characters of the file
3
if f.read().endswith((".html", "_files")):
4
# whatever
5