Have run across the problem with this task:
In a file called extensions.py, implement a program that prompts the user for the name of a file and then outputs that file’s media type if the file’s name ends, case-insensitively, in any of these suffixes:
.gif
.jpg
.jpeg
.png
.pdf
.txt
.zip
If the file’s name ends with some other suffix or has no suffix at all, output application/octet-stream instead, which is a common default.
My code is below. Is it apropriate stylisticly or should I improve is somehow?
ext = input("Type the extension of a file: ")
if ext.lower().strip().endswith(".gif"):
print("image/gif")
elif ext.lower().strip().endswith(".jpg"):
print("image/jpeg")
elif ext.lower().strip().endswith(".jpeg"):
print("image/jpeg")
elif ext.lower().strip().endswith(".png"):
print("image/png")
elif ext.lower().strip().endswith("pdf"):
print("application/pdf")
elif ext.lower().strip().endswith(".txt"):
print("text/plain")
elif ext.lower().strip().endswith(".zip"):
print("application/zip")
else:
print("application/octet-stream")
Lynn M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4