利用了os的remove还有递归函数调用:
import os
import shutil
def del_file(path):
if not os.listdir(path):
print('目录为空!')
else:
for i in os.listdir(path):
path_file = os.path.join(path,i) #取文件绝对路径
print(path_file)
if os.path.isfile(path_file):
os.remove(path_file)
else:
del_file(path_file)
shutil.rmtree(path_file)
if __name__ == '__main__':
path=r'c:\Users\HP\desktop\1'
del_file(path)
定义函数,先判断是否为空,然后先删除文件,再递归删除文件夹