1. 程式人生 > >python-18 os 模組

python-18 os 模組

刪除檔案 建立資料夾 刪除資料夾

remove 刪除檔案 exists 判斷檔案或者資料夾是否存在 path 路徑

刪除檔案

import os
filepath = r"D:\\data1.txt"
isexist = os.path.exists(filepath)
if isexist:
    os.remove(filepath)
    print("檔案已經刪除")

else:
    print("檔案不存在")


結果:
檔案已經刪除

注意:路徑前面加上r