Take string from an input, and pick out part of that text to print its definition from a dictionary

Question:

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?

Asked By: CoDiNg_76

||

Answers:

If the file name is really just the file name with one dot in it, then you can use .split() to get the extension:

file_extension = file_name.split('.')[-1]

By way of explanation: split.('.') splits up the string that you’ve entered with the dot as the separator and returns a list, and the [-1] index gives you the last item in that list, i.e., the extension.

You can then look up that extension in your dictionary.

Answered By: Schnitte

That should not be much of an issue.

  • First, create a dictionary of {extension:explanation} items.
  • Then, ask the user to enter the filename.
  • Then, split the file name by dot, take the part after dot. You can use the partition method for that.
  • Finally, query your dictionary and return the output.

Code:

# First, create a dictionary of {extension:explanation} items.
extensions_dict {
    'png': 'image/media'
    # put as much as you wish
}

# Then, ask the user to enter the filename.
user_file = input('file name: ')

# Then, split the file name by dot, take the part after dot.
file_name, dot, file_extension = user_file.partition('.')

# Finally, query your dictionary and return the output.
# You can print a message to the user as a default 
# if the extension is not in your dictionary
print(extensions_dict.get(
  file_extension,
  f'I cannot understand your extension ({file_extension}) :('
)
Answered By: Maged Saeed
import os

file_name = 'cat.png'
extensions = {
    '.gif': 'image/gif',
    '.jpg': 'image/jpeg' ,
    '.jpeg': 'image/jpeg' ,
    '.png': 'image/png' ,
    '.pdf': 'application/text' ,
    '.txt': 'text/plain' ,
    '.zip': 'application/zip'
    }

print(extensions[os.path.splitext(file_name)[1]])

So first of all, I have changed the structure of the extensions – there is no real need to be so repetitive and keep a list of dicts which would be much harder to browse through. Using extensions as keys, you can refer to them directly to find respective format.

Then you can use splitext method from Python os.path module to get a string representing file extensions (which works better for various edge cases than str.split)

Answered By: matszwecja

The other solutions are fine and OK. But I want to offer you a more pythonic way using Pythons pathlib package to handle file path objects the platform independent way and some other tweaks.

#!/usr/bin/env python3
import pathlib  # recommended way with Python3

media_types_dict = {
    'png': 'image/media',
    'gif': 'image/gif',
    # ...
}

# ask the user
user_file = input('file name: ')

# convert to a file path object
user_file = pathlib.Path(user_file)

# get the extension (with the trailing dot, e.g. ".png")
extension = user_file.suffix

# remove the dot
extension = extension[1:]

try:
    # get the media type
    media_type = media_types_dict[extension.lower()]
except KeyError:  # if the extensions is not present in the dict
    raise Exception(f'The extension "{extension}" is unknown.')

# output the result
print(media_type)
Answered By: buhtz

Using split(".") may create error if user inputs without a dot(.) It may not be more pythonic but using if….elif….else would solve this problem.

x=input("enter filename with extension: ").lower()
if x.endswith(".gif"):
    print("image/gif")
elif x.endswith(".jpg"):
    print("image/jpg")
elif x.endswith(".jpeg"):
    print("image/jpeg")
elif x.endswith(".png"):
    print("image/png")
elif x.endswith(".pdf"):
    print("document/pdf")
elif x.endswith(".txt"):
    print("text/txt")
elif x.endswith(".zip"):
    print("archive/zip")
else:
    print("application/octet-stream")
Answered By: Kapil02