使用上傳文字檔取代 stdin

請選取一個附檔名為 .txt 的文字檔, 檔名不能有中文字, 按 [瀏覽]上傳後再按 Try it!

import os
from html import escape
text_files = [filename for filename in os.listdir() if filename.endswith('.txt')]
if len(text_files) == 0:
    print("No '.txt' files found!")
elif len(text_files) != 1:
    print("Please upload only a single .txt file")
else:
    with open(text_files[0]) as infile:
        raw = infile.read()
        if len(raw.strip()) == 0:
            print("EMPTY FILE!")
        else:
            print(escape(raw))


Última modificación: lunes, 20 de marzo de 2023, 23:25