deletefile函數用于刪除指定的文件。其正確使用方法是:
示例代碼如下:
import os
def deletefile(filepath):
try:
os.remove(filepath)
print("File deleted successfully.")
except FileNotFoundError:
print("File not found.")
except PermissionError:
print("Permission denied to delete file.")
# 使用示例
deletefile("example.txt")