-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMIME_type.py
More file actions
70 lines (52 loc) · 1.9 KB
/
MIME_type.py
File metadata and controls
70 lines (52 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Web browsers, rely on MIME types (also known as media types), to determine how to display files that live on the web.
When you download a file from a web server, that server sends an HTTP header, along with the file itself, indicating the file’s media type.
For instance, the media type for a GIF is image/gif, and the media type for a JPEG is image/jpeg.
To determine the media type for a file, a web server typically looks at the file’s extension, mapping one to the other.
"""
extension = input("File name: ").strip().lower()
if "." in extension:
extList = list(extension.split("."))
L = len(extList)
if(extList[L-1] in ["png", "gif", "jpeg"]):
print(f"image/{extList[L-1]}")
elif(extList[L-1] == "jpg"):
print("image/jpeg")
elif(extList[L-1] == "pdf" or extList[L-1] == "zip"):
print(f"application/{extList[L-1]}")
elif(extList[L-1] == "txt"):
print(f"text/plain")
else:
print("application/octet-stream")
elif "." not in extension:
print("application/octet-stream")
# ---------------------------------------------------
# OTHER METHOD
mime_types = {
"png": "image/png",
"jpeg": "image/jpeg",
"gif": "image/gif",
"jpg": "image/jpeg",
"pdf": "application/pdf",
"zip": "application/zip",
"txt": "text/plain"
#Add More....
}
file = input("File name: ").strip().lower()
if "." in file:
file_parts = file.split(".")
file_extension = file_parts[-1]
print(mime_types.get(file_extension, "application/octet-stream"))
else:
print("application/octet-stream")
# ----------------------------------------------------------------------
# OTHER METHOD (easiest)
#(Considers all the extensions taht exist)
import mimetypes
file = input("File name: ").strip().lower()
result = mimetypes.guess_type(file)
mime_type = result[0]
if mime_type:
print(mime_type)
else:
print("application/octet-stream")