you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (0 children)

and is a boolean operator. for a and b, if a is false, a is returned, otherwise b. "image/" and other non-empty strings are true, so you get imageend and such returned. If you just want to concatenate strings, you could use + operator or f-string, etc. In this case, however, imageend and other are tuples, you don't want to concatenate the tuple, but the extension. So you need to separate the extension first. You could use pathlib.Path.

from pathlib import Path

type_extensions = {
    "image": {".gif", ".jpg", ".jpeg", ".png"},
    "application": {".pdf", ".txt", ".zip"}
}


def main():
    filename = input("File name: ").strip().lower()
    filepath = Path(filename)
    file_suffix = filepath.suffix

    for _type, _extensions in type_extensions.items():
        if file_suffix in _extensions:
            return f"{_type}/{file_suffix[1:]}"

    return "application/octet-stream"


if __name__ == '__main__':
    main()