按扩展名定类型

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

一张扩展名到类型的表。看几个文件各配什么类型,没登记的一律 application/octet-stream:

TYPES = {".css": "text/css", ".js": "application/javascript", ".png": "image/png", ".html": "text/html", ".json": "application/json"}

def ctype(name):
    dot = name.rfind(".")
    return TYPES.get(name[dot:] if dot >= 0 else "", "application/octet-stream")

print(",".join(ctype(n) for n in ("style.css", "app.js", "logo.png", "README", "data.json")))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论