Python: fetch filename suffix

Miss Discontinuity
1 min readAug 13, 2020

--

Python day 25:

When i was younger, I only dealt with latex, docx, pdf files. Since i started blogging, i use a lot more files such as jpg, peg, mov4, ipynb, py, md, sas, r , HTML.
Sometimes, it is a bit difficult to organize the files.
Today we are going to make a small python program to fetch the suffix.

Little Goal: Use python to fetch the filename suffix.

[caption id=”attachment_3740" align=”alignnone” width=”270"]

yibo.gif[/caption]

def suffix(filename, has_dot=False):
pos=filename.rfind(".")
if 0< pos <len(filename) -1:
index= pos if has_dot else pos +1
return filename [index:]
else:
return " "
suffix("yibo.gif")
'gif'

[caption id=”attachment_3743" align=”alignnone” width=”225"]

yibo.jpg[/caption]

suffix("yibo.jpg")
'jpg'

Happy Studying!

Reference:

https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md

--

--