免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1896 | 回复: 0
打印 上一主题 下一主题

列出特定文件夹下面小于特定大小的文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-30 16:54 |只看该作者 |倒序浏览
#!/usr/bin/env python

"""
FindSmallFile.py

SYNOPSIS

    Print the file name which size is small
    than the cutoff value in the given path.

DESCRIPTION

    python FindSmallFile.py -d path_to_file -e cutoff_value

OPTIONS
    -e : the cutoff value (eg. 1K, 2.5K, 1M, 2,G ...)
    -d : the path of the file in (eg. /home/quwb/)

    -? : display help (--? and --help do the same thing).

    -v : verbose output

EXAMPLES

    python FindSmallFile.py -d ./ -e 1.2k

AUTHOR

    Wubin Qu

LICENSE

    This script is under the GPL licenses.

"""

import sys, os, getopt, traceback
import time
import re
import string

VERBOSE = False

def exit_with_usage ():

    print globals()['__doc__']
    os._exit(1)

def parse_args (options='', long_options=[]):

    try:
        optlist, args = getopt.getopt(sys.argv[1:], options+'?', long_options+['help','?'])
    except Exception, e:
        print str(e)
        exit_with_usage()
    options = dict(optlist)
    if [elem for elem in options if elem in ['-?','--?','--help']]:
        exit_with_usage()
    return (options, args)

def main ():

    global VERBOSE

    (options, args) = parse_args('vd:e:')
    if '-v' in options:
        VERBOSE = True
    else:
        VERBOSE = False
   
    if options.has_key('-d') and options['-d'] != '':
    path = options['-d']
    else:
    print 'option "-d" is needed'
    exit_with_usage()
    if options.has_key('-e') and options['-e'] != '':
    cutoff = options['-e']
    else:
    print 'option "-e" is needed'
    exit_with_usage()
    cutoff = str(cutoff)
    if cutoff.find('K') >= 0:
    cutoff = re.sub('K', '', cutoff)
    cutoff = string.atoi(cutoff) * 1024
    elif cutoff.find('M') >= 0:
    cutoff = re.sub('M', '', cutoff)
    cutoff = string.atoi(cutoff) * 1024**2
    elif cutoff.find('G') >= 0:
    cutoff = re.sub('G', '', cutoff)
    cutoff = string.atoi(cutoff) * 1024**3
    elif re.search('\D', cutoff):
    print 'cutoff arguments contains some odd characters'
    exit_with_usage()
    else:
    cutoff = string.atoi(cutoff)
   
    for root, dirs, files in os.walk(path):
    for file in files:
        size = os.path.getsize(file)
        if not size:
        print '%s size: 0.' % file
        else:
        if size <= cutoff:
            print '%s size: %d' % (file, size)
   

if __name__ == '__main__':
    try:
        start_time = time.time()
        if VERBOSE: print time.asctime()
        main()
        if VERBOSE: print time.asctime()
        if VERBOSE: print 'TOTAL TIME IN MINUTES:',
        if VERBOSE: print (time.time() - start_time) / 60.0
        sys.exit(0)
    except SystemExit, e:
        raise e
    except Exception, e:
        print 'ERROR, UNEXPECTED EXCEPTION'
        print str(e)
        traceback.print_exc()
        os._exit(1)


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/64942/showart_512003.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP