当前位置:Gxlcms > Python > python使用fileinput模块实现逐行读取文件的方法

python使用fileinput模块实现逐行读取文件的方法

时间:2021-07-01 10:21:17 帮助过:26人阅读

本文实例讲述了python使用fileinput模块实现逐行读取文件的方法。分享给大家供大家参考。具体实现方法如下:

  1. #--------------------------------
  2. # Name: read_lines.py
  3. # Author: Kevin Harris
  4. # Last Modified: 02/13/04
  5. # Description: This Python script demonstrates
  6. # how to use fileinput to read
  7. # each line of a given file.
  8. #--------------------------------
  9. import fileinput
  10. for line in fileinput.input( "test.txt" ):
  11. print line,
  12. raw_input( '\n\nPress Enter to exit...' )

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行