美文网首页
python删除指定文件夹下所有内容

python删除指定文件夹下所有内容

作者: AS氕氘氚 | 来源:发表于2018-07-22 12:16 被阅读0次
import os
import glob

# folders location
path = r'C:\Users\V310\Desktop\123'

def delfile( path):
    
#   read all the files under the folder
    fileNames = glob.glob(path + r'\*')
    
    for fileName in fileNames:
        try:
#           delete file
            os.remove( fileName)
        except:
            try:
#               delete empty folders 
                os.rmdir( fileName)
            except:
#               Not empty, delete files under folders  
                delfile( fileName)
#               now, folders are empty, delete it  
                os.rmdir( fileName)

delfile( path)


相关文章

网友评论

      本文标题:python删除指定文件夹下所有内容

      本文链接:https://www.haomeiwen.com/subject/srylmftx.html