python实现将文件夹/子文件夹中内容清空

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yql_617540298/article/details/82025048

一、写在前面

需求:若有很多文件夹/子文件夹,需要将里面的内容清空,若将里面全部图片删掉,用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)

三、结果

可以得到文件夹/子文件夹空。

猜你喜欢

转载自blog.csdn.net/yql_617540298/article/details/82025048