|
发表于 2025-5-9 01:55:48
|
显示全部楼层
本帖最后由 jackz007 于 2025-5-9 02:06 编辑
- import os
- p = input('请输入目标文件夹:') . strip()
- if p and os . path . exists(p) and os . path . isdir(p) :
- q = input('你真的要删除路径 [%s] 下所有的文件【N】?:' % p)
- if q . lower() == 'y' :
- c , e = 0 , 0
- for root , dirs , files in os . walk(p) :
- for file in files :
- x = os . path . join(root , file)
- try:
- os . remove(x)
- print('[%4d] - %s 【删除成功】 ' % (c + 1 , x))
- c += 1
- except :
- print('[%4d] - %s * [删除失败] * ' % (c + 1 , x))
- e += 1
- print()
- print('总共找到了 [%d] 个文件,成功删除了 [%d] 个文件。' % (e , c))
复制代码
警告:这个代码将删除指定路径及其所有子目录下的所有普通文件,但是,所有的子目录会得到保留。,所以,运行前,你要记得先行备份那些即将被删除的文件,以免遭受不必要的损失。 |
|