为了清理指定目录中的文件,您可以使用以下Python代码来实现:
import os
def clean_directory(directory):
files = os.listdir(directory)
for file in files:
file_path = os.path.join(directory, file)
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Removed file: {file_path}")
# 指定要清理的目录路径
directory_path = "path_to_directory"
clean_directory(directory_path)
请确保将 path_to_directory 替换为要清理的目录的实际路径。请注意,这段代码将删除指定目录中的所有文件。如果您需要根据特定条件进行文件清理,请在 clean_directory 函数中添加适当的逻辑。
2010

被折叠的 条评论
为什么被折叠?



