python列举文件夹下所有文件行数

2023-01-31 05:01:16 文件夹 行数 列举

    在群里聊天,看到一段代码,是可以统计文件夹下所有文件的行数,觉得不错,记录下来,以备将来有用的时候来派用场,下面贴上代码:

 

  1. import os 
  2. import fileinput 
  3. dest="/root/log" 
  4. for root,dirs,filename in os.walk(dest): 
  5.     for file in filename: 
  6.         f=os.path.join(root,file) 
  7.         for each in fileinput.input(f): 
  8.             pass 
  9.         print "filename:%s    filenumber:%s" % (fileinput.filename(), fileinput.lineno())  

 下面看下效果:

 

  1. [root@qhz ~]# python list_number.py 
  2. filename:/var/log/secure    filenumber:28043 
  3. filename:/var/log/cron    filenumber:550 
  4. filename:/var/log/cron.4    filenumber:149 
  5. filename:/var/log/maillog.3    filenumber:2 
  6. filename:/var/log/messages.1    filenumber:5058 
  7. filename:/var/log/secure.4    filenumber:11577 
  8. filename:/var/log/maillog.2    filenumber:0 
  9. filename:/var/log/secure.1    filenumber:11415 
  10. filename:/var/log/secure.3    filenumber:27105 
  11. filename:/var/log/wtmp    filenumber:23 
  12. filename:/var/log/rpmpkgs.1    filenumber:249 
  13. filename:/var/log/spooler.2    filenumber:0 
  14. filename:/var/log/rpmpkgs.3    filenumber:247 
  15. filename:/var/log/lastlog    filenumber:1 
  16. filename:/var/log/spooler    filenumber:0 
  17. filename:/var/log/xferlog.1    filenumber:0 
  18. filename:/var/log/xferlog.4    filenumber:0 
  19. filename:/var/log/boot.log.1    filenumber:0 
  20. filename:/var/log/spooler.4    filenumber:0 
  21. filename:/var/log/secure.2    filenumber:22174 
  22. filename:/var/log/maillog.4    filenumber:0 
  23. filename:/var/log/boot.log.2    filenumber:0 
  24. filename:/var/log/cron.1    filenumber:1191 
  25. filename:/var/log/maillog.1    filenumber:0 
  26. filename:/var/log/yum.log    filenumber:112 
  27. filename:/var/log/boot.log.3    filenumber:0 
  28. filename:/var/log/boot.log.4    filenumber:0 
  29. filename:/var/log/cron.2    filenumber:1019 
  30. filename:/var/log/btmp    filenumber:3793 
  31. filename:/var/log/rpmpkgs.4    filenumber:166 
  32. filename:/var/log/messages.2    filenumber:7992 
  33. filename:/var/log/faillog    filenumber:1 
  34. filename:/var/log/xferlog    filenumber:0 
  35. filename:/var/log/maillog    filenumber:0 
  36. filename:/var/log/spooler.3    filenumber:0 
  37. filename:/var/log/tallylog    filenumber:0 
  38. filename:/var/log/spooler.1    filenumber:0 
  39. filename:/var/log/boot.log    filenumber:0 
  40. filename:/var/log/xferlog.3    filenumber:0 
  41. filename:/var/log/rpmpkgs    filenumber:249 
  42. filename:/var/log/cron.3    filenumber:177 
  43. filename:/var/log/messages.3    filenumber:10126 
  44. filename:/var/log/xferlog.2    filenumber:0 
  45. filename:/var/log/dmesg    filenumber:127 
  46. filename:/var/log/messages    filenumber:11158 
  47. filename:/var/log/rpmpkgs.2    filenumber:249 
  48. filename:/var/log/messages.4    filenumber:5838 
  49. filename:/var/log/mail/statistics    filenumber:0 
  50. filename:/var/log/.hist/root/203.95.5.81.hist.20111117_165816    filenumber:22 
  51. filename:/var/log/.hist/root/119.6.66.226.hist.20111115_111319    filenumber:32 
  52. filename:/var/log/.hist/root/203.95.5.81.hist.20111115_110023    filenumber:52 
  53. filename:/var/log/.hist/root/222.65.30.17.hist.20111114_213102    filenumber:26 
  54. filename:/var/log/.hist/root/222.65.96.54.hist.20111125_232235    filenumber:4 
  55. filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184102    filenumber:46 
  56. filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184612    filenumber:14 
  57. filename:/var/log/.hist/root/203.95.5.81.hist.20111122_093004    filenumber:4 
  58. filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184442    filenumber:12 
  59. filename:/var/log/.hist/root/218.82.186.138.hist.20111113_184315    filenumber:20 

   效果还是不错的,更多Python代码,可以到www.50rescue.com上看看

相关文章