From 19ec34aa1de569ece856d97dd1ed66fc1b7910e1 Mon Sep 17 00:00:00 2001 From: ChenKaiLiuG <141424456+ChenKaiLiuG@users.noreply.github.com> Date: Tue, 1 Apr 2025 00:49:03 +0800 Subject: [PATCH] Create print.py --- print.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 print.py diff --git a/print.py b/print.py new file mode 100644 index 0000000..b18aeb7 --- /dev/null +++ b/print.py @@ -0,0 +1,20 @@ +##test +import os + +def read_and_print_file(filename): + """讀取並輸出檔案內容""" + try: + with open(filename, 'r', encoding='utf-8') as file: + for line in file: + print(line, end='') # end='' 避免多餘的換行 + except FileNotFoundError: + print(f"找不到檔案:{filename}") + except Exception as e: + print(f"讀取檔案時發生錯誤:{e}") + +if __name__ == "__main__": + filename = "找找自己問題.txt" + if os.path.exists(filename): #檢查檔案是否存在。 + read_and_print_file(filename) + else: + print(f"檔案 {filename} 不存在於當前目錄。")