Python中使用标准库中的linecache中的getline方法可以从某个文件中读取出特定的一行。比如:
Python天天美味(18) - linecache.getline()读取文件中特定一行import linecache
Python天天美味(18) - linecache.getline()读取文件中特定一行
print linecache.getline('2.1_open.py'4)

将返回我上一节事例代码文件2.1_open.py的第4行文字,输出结果:
f = open('/home/evergreen/桌面/test')

查看linecache中的实现(我用的是Ulipad,所以直接将光标停留在linecache处,按F6键)

linecache.py

 

Python 天天美味系列(总)

Python 天天美味(16) - 过滤字符串的技巧,map与itertools.imap  

Python 天天美味(17) - open读写文件  

Python 天天美味(18) - linecache.getline()读取文件中特定一行  

Python 天天美味(19) - 时间处理datetime  

Python 天天美味(20) - 命令行参数sys.argv   

...

相关文章:

  • 2021-08-03
  • 2021-12-27
  • 2021-06-13
  • 2021-10-07
  • 2022-01-28
  • 2021-12-04
  • 2021-05-28
猜你喜欢
  • 2021-06-25
  • 2021-10-09
  • 2021-10-11
  • 2021-08-17
  • 2021-08-15
  • 2022-01-15
相关资源
相似解决方案