//注意Linux主机
//删除指定目录中文件名以 .tmp 为后缀的文件
1 #!/usr/bin/python 2 import os , glob 3 dirname = ‘/tmp‘ 4 allpy = glob.glob(os.path.join(dirname,‘*‘)) 5 for filename in allpy: 6 filesize = os.path.getsize(filename) 7 if(filename.endswith(‘tmp‘)): 8 try: 9 os.remove(filename) 10 except: 11 continue
//输出指定文件夹(包括子文件夹)中最大的两个文件
1 #!/usr/bin/python 2 import os 3 dirname = ‘/tmp‘ 4 allsize = [] 5 for (root,dirs,files) in os.walk(dirname): 6 for filename in files: 7 fullname = os.path.join(root,filename) 8 filesize = os.path.getsize(fullname) 9 allsize.append((filesize,fullname)) 10 allsize.sort() 11 print allsize[-2:]
//删除指定文件(包括子文件夹)中的所有文件及文件夹
1 #!/usr/bin/python 2 import os 3 dirname = ‘/tmp‘ 4 allsize = [] 5 for (root,dirs,files) in os.walk(dirname): 6 for filename in files: 7 fullname = os.path.join(root,filename) 8 filesize = os.path.getsize(fullname) 9 allsize.append((filesize,fullname)) 10 allsize.sort() 11 print allsize[-2:]
时间: 2024-11-05 12:25:24