0

I am passing a file object as an argument to a function in Python. Inside this function I want to read the content of the file passed as an argument.

Will I be able to read the file from the beginning or will it continue from the last line read before passing it to the current function ?

2
  • 1
    from the next line... Commented Jun 26, 2015 at 18:58
  • It is easy to test... Commented Jun 26, 2015 at 19:04

4 Answers 4

6

This is something you could easily find out yourself by testing. The filehandle remembers it's current line so it will continue reading where it left of.

Sign up to request clarification or add additional context in comments.

1 Comment

I wish I could upvote this answer twice or more - why do people waiste everyone's time asking questions they could answer by themselves in less time than they spent posting the question, I really wonder...
0

I haven't coded in python much, but I would assume that the parameter you passed in is actually a reference to the object, which means that it should read from the next line. See this post for clarification.

Comments

0

File objects are generators, so they will continue from the next line.

3 Comments

"Hence why you can't use them once read or readlines has been called" : you may want to read about file.seek(0).
"they will continue from the next line. " : unless you call .seek(0) which will reset the pointer at the beginning of the file
"it will continue where it left off" unless you reset the file pointer using file.seek()
0

You might find https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects helpful. The file object keeps track of where it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.