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

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删除指定文件夹下所有内容)