一、写在前面
需求:若有很多文件夹/子文件夹,需要将里面的内容清空,若将里面全部图片删掉,用python实现可以简化操作。
二、源码
import os
path = 'C:/Users/Administrator/Desktop/test'
for i in os.listdir(path):
path_file = os.path.join(path,i)
if os.path.isfile(path_file):
os.remove(path_file)
else:
for f in os.listdir(path_file):
path_file2 =os.path.join(path_file,f)
if os.path.isfile(path_file2):
os.remove(path_file2)
三、结果
可以得到文件夹/子文件夹空。