I'm relatively new to python, I have made a few pieces of very simple code, and I'm struggling to implement a program that prompts the user for the name of a file (e.g. : cat.png) and then outputs that file’s media type (e.g. : .png). If the file’s name ends, case-insensitively, in any of these suffixes:
-.gif
-.jpg
-.jpeg
-.png
-.pdf
-.txt
-.zip
Then I want to print out its corresponding meaning:
-image/gif
-image/jpeg
-image/jpeg
-image/png
-application/text
-text/plain
-application/zip
e.g. :
My desired output:
$ python extensions.py
File name: cat.gif
image/gif
$ python extensions.py
File name: cat.jpg
image/jpg
I've tried to solve this problem using a dictionary, to match a name to its corresponding format:
file_name = input('File name: ').strip().lower()
extensions = [
{'name': '.gif', 'format': 'image/gif' },
{'name': '.jpg', 'format': 'image/jpeg' },
{'name': '.jpeg', 'format' :'image/jpeg' },
{'name': '.png', 'format': 'image/png' },
{'name': '.pdf', 'format': 'application/text' },
{'name': '.txt', 'format': 'text/plain' },
{'name': '.zip', 'format': 'application/zip' }
]
Problem is, I don't know how to turn a user output like cat.png into a file like .png and printed on the terminal as image/png like the picture above. I'm trying to find a way to somehow somehow take the .png part out of the 'cat.png', and pass it through a dictionary, printing out the image/png.
Appreciate you reading this long description. Anyone have ideas to implement such a program maybe?