Reputation: 11
I am currently in the process of learning how to program in Python, and I really don't understand how the "open()" function works. I somewhat know how to use it, but I don't understand it very well. The question that I'm mainly asking is : what's going on in the background when you are using the "open()" function and what is happening when you assign it to a variable?
Doing research has given me little to no help. The only thing I have leaned is that when a "open()" function is assigned to a variable, a file object is assigned to that variable, which is a bunch of methods and variables. But researching more about this hasn't helped me. What are specifically these methods and variables?
I've tried doing tests to understand more, but this just leaves me with more questions than answers. For example :
When you execute ".read()" on the same file, and assign to 2 different variables, only the first ".read()" will actually assign properly whats in the file to the first variable, but the second one will assign nothing. What happened there? The following code shows this problem:
makeFile = open("test.txt","w")
makeFile.write("Hello!")
makeFile.close()
makeFile = open("test.txt","r")
read1 = makeFile.read()
read2 = makeFile.read()
print("first variable:",read1)
print("second variable:",read2)
makeFile.close()
input("Press enter to finnish program")
Here's the output:
first variable: Hello!
second variable:
Press enter to finish program
I know I could simply assign "read1" to "read2", but why is it assigning nothing to the second variable? I expect it to output the same string in both of the variables, but it only shows it once, and then the other one shows nothing.
I'm sorry if I'm asking too many questions for this, but I would help me a lot if somebody explained what is going on when I use the "open()" function and why that example didn't work as attended.
Upvotes: 0
Views: 253
Reputation: 1690
Initially the "cursor" is at the beginning of the file. makeFile.read()
moves the cursor to the end of file (because it reads all the content of the file). when you execute makeFile.read()
the second time, the cursor is still at the end of the file, so there is nothing to read.
You can use the seek()
function to move the cursor.
Upvotes: 2
Reputation: 2136
The key here is understanding what you are doing when you assign r1 and r2. You are calling the read method twice, but the second time the file has already been read to the end and there is nothing further to read. All subsequent calls to this method will return nothing - it's not a stateless operation. Perhaps it will help if you observe that the result is the same without the variable assignment.
makeFile = open("test.txt","w")
makeFile.write("Hello!")
makeFile.close()
makeFile = open("test.txt","r")
print("first variable:",makeFile.read())
print("second variable:",makeFile.read())
makeFile.close()
input("Press enter to finnish program")
But if you add another call to .read() first you get nothing in both prints:
makeFile = open("test.txt","w")
makeFile.write("Hello!")
makeFile.close()
makeFile = open("test.txt","r")
makeFile.read()
print("first variable:",makeFile.read())
print("second variable:",makeFile.read())
makeFile.close()
input("Press enter to finnish program")
Upvotes: 2