1. 程式人生 > >python刪除某路徑下的檔案

python刪除某路徑下的檔案

import os
# import traceback
# os.system('start "" /d "C:\Windows" "regedit.exe"')

my_file = 'D:/hha.txt' # 檔案路徑
if os.path.exists(my_file): # 如果檔案存在
    #刪除檔案,可使用以下兩種方法。
    os.remove(my_file) # 則刪除
    #os.unlink(my_file)
else:
    print('no such file:%s'%my_file)