Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

html

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

like image 926
user1434001 Avatar asked Nov 29 '25 23:11

user1434001


1 Answers

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
like image 200
Sven Marnach Avatar answered Dec 01 '25 14:12

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!