首页 > 代码库 > 删除一定大小的文件

删除一定大小的文件

import os
import os.path
filePath = input(‘Enter filepath : ‘)
size=int(input(‘Enter the max size you want to delete(KB):‘))
#三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for parent ,dirnames , filenames in os.walk(filePath):
# for dirname in dirnames: #这些用不到
# print( ‘parent is :‘+parent) #这些用不到
# print (‘dirname is ‘+ dirname #这些用不到

for filename in filenames:
print(‘parent is :‘ + parent)
print(‘filename is :‘ + filename)
currentPath = os.path.join(parent, filename)
print(‘the fulll name of the file is :‘ + currentPath)
filesize = os.path.getsize(currentPath) / 1024
print(‘the file size is : %.3f KB‘ % (filesize))
if filesize<size:
os.remove(currentPath)

删除一定大小的文件