1. 使用`os.remove()`函数删除文件。在尝试删除之前,请确保文件存在。
import osfile_path = 'path_to_your_python_file.py'if os.path.exists(file_path):os.remove(file_path)else:print('文件不存在,无法删除!')
2. 使用`with open()`语句和`file.truncate(0)`清空文件内容。

with open('path_to_your_python_file.py', 'r') as file:file.truncate(0)
3. 使用`shutil.rmtree()`函数删除整个文件夹,包括其中的所有文件和子文件夹。
import shutilfolder_path = 'path_to_your_python_directory'if os.path.exists(folder_path):shutil.rmtree(folder_path)else:print('文件夹不存在,无法删除!')
请确保替换`path_to_your_python_file.py`和`path_to_your_python_directory`为实际的文件或文件夹路径。
