Есть ли способ сократить мой код и избежать повторения расширений и условий? Пробую проблему, поставил "расширения" от CPython

Программы на Python
Anonymous
Есть ли способ сократить мой код и избежать повторения расширений и условий? Пробую проблему, поставил "расширения" от C

Сообщение Anonymous »

Код: Выделить всё

file = input("File name: ").strip().lower()

extension_image = (".gif",".jpg", ".jpeg",".png")
extension_application = (".pdf","zip")
extension_text = ("txt")

condition_image = file.endswith(extension_image)
condition_application = file.endswith(extension_application)
condition_text = file.endswith(extension_text)

if condition_image == True:
normal = file.split(".")
print("image/" + str(normal[-1]))
elif condition_application == True:
normal = file.split(".")
print("application/" + str(normal[-1]))
elif condition_text == True:
normal = file.split(".")
print("text/" + str(normal[-1]))
else:
print("application/octet-stream")
Файл принимает в качестве входных данных имя и расширение, и я должен заменить расширение, например (.pdf), типом файловой косой черты расширения.

Подробнее здесь: https://stackoverflow.com/questions/787 ... repetition

Вернуться в «Python»