python小练——找出指定目录下小于指定字节的文件,输出到文本文件

代码:

import os
import sys
def showsmallfiles(dir):
    v=os.listdir(dir)
    for i in v:
        d="%s/%s"% (dir,i)
        if os.path.isdir(d):
            showsmallfiles(d)
        else:
            if os.path.getsize(d) < int(sys.argv[2]):
                logger(d)

#logfile
def logger(text):
    f=open('d:/python/log.txt','a')
    f.write(text+'\r\n')
    f.close()

if len(sys.argv)<3:
    print('arguments is invalid')
    sys.exit()

showsmallfiles(sys.argv[1])

调用方式:

python listsmallfile.py d:/python 1000

原文地址:https://www.cnblogs.com/lyroge/p/2133504.html