Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

Go to a specific line in Python?

I want to go to line 34 in a .txt file and read it. How would you do that in Python?

question from:https://stackoverflow.com/questions/2444538/go-to-a-specific-line-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Python Standard Library's linecache module:

line = linecache.getline(thefilename, 33)

should do exactly what you want. You don't even need to open the file -- linecache does it all for you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...