os判断文件夹是否存在,若存在删除,重新创建文件夹
filePath = r'C:\Users\Administrator\Desktop\step2' # 文件路径
if os.path.exists(filePath):
for fileList in os.walk(filePath):
for name in fileList[2]:
os.chmod(os.path.join(fileList[0], name), stat.S_IWRITE)
os.remove(os.path.join(fileList[0], name))
shutil.rmtree(filePath)
os.mkdir(filePath)
本文介绍使用Python的os模块判断指定路径的文件夹是否存在,如果存在则先删除所有子文件并更改权限,然后删除文件夹,最后重新创建该文件夹。

1483

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



