Reputation: 21
I am unable to read the data from file in python . Below is the sample code and Error which I am getting .
abc.txt has the value 2015-05-07
f = open("/opt/test/abc.txt","r")
f.read()
last_Exe_date = f.read()
f.close()
while reading the file(anc.txt) i am getting the error : TypeError: argument 1 must be string or read-only character buffer, not file . i am not able to read the value into last_Exe_date from file(abc.txt) . could you please correct me if I am wrong with the code.
Upvotes: 1
Views: 641
Reputation: 46759
The following should work fine:
f = open("/opt/test/abc.txt","r")
last_Exe_date = f.read()
f.close()
As was said, you had f.read()
twice, so when you tried to store the contents into last_Exe_date
, it would be empty.
You could also consider using the following method:
with open("/opt/test/abc.txt","r") as f:
last_Exe_date = f.read()
This will ensure that the file is automatically closed afterwards.
Upvotes: 0
Reputation: 4186
When you read a file once, the cursor is at the end of the file and you won't get anything more by re-reading it. Read through the docs to understand it more. And use readline
to read a file line by line.
Oh, and remove the semicolon at the end of your read
calls...
Upvotes: 4