python列举文件夹下所有文件行数
在群里聊天,看到一段代码,是可以统计文件夹下所有文件的行数,觉得不错,记录下来,以备将来有用的时候来派用场,下面贴上代码:
- import os
- import fileinput
- dest="/root/log"
- for root,dirs,filename in os.walk(dest):
- for file in filename:
- f=os.path.join(root,file)
- for each in fileinput.input(f):
- pass
- print "filename:%s filenumber:%s" % (fileinput.filename(), fileinput.lineno())
下面看下效果:
- [root@qhz ~]# python list_number.py
- filename:/var/log/secure filenumber:28043
- filename:/var/log/cron filenumber:550
- filename:/var/log/cron.4 filenumber:149
- filename:/var/log/maillog.3 filenumber:2
- filename:/var/log/messages.1 filenumber:5058
- filename:/var/log/secure.4 filenumber:11577
- filename:/var/log/maillog.2 filenumber:0
- filename:/var/log/secure.1 filenumber:11415
- filename:/var/log/secure.3 filenumber:27105
- filename:/var/log/wtmp filenumber:23
- filename:/var/log/rpmpkgs.1 filenumber:249
- filename:/var/log/spooler.2 filenumber:0
- filename:/var/log/rpmpkgs.3 filenumber:247
- filename:/var/log/lastlog filenumber:1
- filename:/var/log/spooler filenumber:0
- filename:/var/log/xferlog.1 filenumber:0
- filename:/var/log/xferlog.4 filenumber:0
- filename:/var/log/boot.log.1 filenumber:0
- filename:/var/log/spooler.4 filenumber:0
- filename:/var/log/secure.2 filenumber:22174
- filename:/var/log/maillog.4 filenumber:0
- filename:/var/log/boot.log.2 filenumber:0
- filename:/var/log/cron.1 filenumber:1191
- filename:/var/log/maillog.1 filenumber:0
- filename:/var/log/yum.log filenumber:112
- filename:/var/log/boot.log.3 filenumber:0
- filename:/var/log/boot.log.4 filenumber:0
- filename:/var/log/cron.2 filenumber:1019
- filename:/var/log/btmp filenumber:3793
- filename:/var/log/rpmpkgs.4 filenumber:166
- filename:/var/log/messages.2 filenumber:7992
- filename:/var/log/faillog filenumber:1
- filename:/var/log/xferlog filenumber:0
- filename:/var/log/maillog filenumber:0
- filename:/var/log/spooler.3 filenumber:0
- filename:/var/log/tallylog filenumber:0
- filename:/var/log/spooler.1 filenumber:0
- filename:/var/log/boot.log filenumber:0
- filename:/var/log/xferlog.3 filenumber:0
- filename:/var/log/rpmpkgs filenumber:249
- filename:/var/log/cron.3 filenumber:177
- filename:/var/log/messages.3 filenumber:10126
- filename:/var/log/xferlog.2 filenumber:0
- filename:/var/log/dmesg filenumber:127
- filename:/var/log/messages filenumber:11158
- filename:/var/log/rpmpkgs.2 filenumber:249
- filename:/var/log/messages.4 filenumber:5838
- filename:/var/log/mail/statistics filenumber:0
- filename:/var/log/.hist/root/203.95.5.81.hist.20111117_165816 filenumber:22
- filename:/var/log/.hist/root/119.6.66.226.hist.20111115_111319 filenumber:32
- filename:/var/log/.hist/root/203.95.5.81.hist.20111115_110023 filenumber:52
- filename:/var/log/.hist/root/222.65.30.17.hist.20111114_213102 filenumber:26
- filename:/var/log/.hist/root/222.65.96.54.hist.20111125_232235 filenumber:4
- filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184102 filenumber:46
- filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184612 filenumber:14
- filename:/var/log/.hist/root/203.95.5.81.hist.20111122_093004 filenumber:4
- filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184442 filenumber:12
- filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184315 filenumber:20
效果还是不错的,更多Python代码,可以到www.50rescue.com上看看
相关文章