I have 2 simple questions about python:
1.How to get number of lines of a file in python?
2.How to locate the position in a file object to the
last line easily?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
lines are just data delimited by the newline char
'\n'.1) Since lines are variable length, you have to read the entire file to know where the newline chars are, so you can count how many lines:
2) reading a chunk from the end of the file is the fastest method to find the last newline char.
You can use that like this: