Home  >  Article  >  Backend Development  >  Python linecache.getline() script to read a specific line in the file

Python linecache.getline() script to read a specific line in the file

巴扎黑
巴扎黑Original
2017-05-21 11:12:424170browse

In Python, you can use the getline method in linecache in the standard library to read a specific line from a file.

For example:

import linecache
print linecache.getline('2.1_open.py', 4)

## will return The 4th line of text in the example code file 2.1_open.py of my previous section, the output result:
f = open('/home/evergreen/Desktop/test')

View the implementation in linecache ( I am using Ulipad, so just put the cursor at linecache and press F6)


The above is the detailed content of Python linecache.getline() script to read a specific line in the file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn