def remove(path):
""" param <path> could either be relative or absolute. """
if os.path.isfile(path):
os.remove(path) # remove the file
elif os.path.isdir(path):
shutil.rmtree(path) # remove dir and all contains
else:
raise ValueError("file {} is not a file or dir.".format(path))
os.remove()
删除一个文件。os.rmdir()
删除一个空目录。shutil.rmtree()
删除一个目录和其所有内容。来自 Python 3.4+
pathlib
模块的Path
对象也暴露了这些实例方法。pathlib.Path.unlink()
删除一个文件或符号链接。pathlib.Path.rmdir()
删除一个空目录。使用
(见shutil上的完整文档)和/或
和
(完整的文档在os上。)
为你们创建一个功能。