Reputation: 13
f = open(r"C:\Users\raghu\3D Objects\myDJZ.py","w+")
f.read(f)
This is my code.. I don't understand what I am doing wrong
Upvotes: 0
Views: 939
Reputation: 382
f.read(4)
means you only want to read 4 bytes. Since you pass an TextIOWrapper
instance to the read function, you get this error because there is no integer representation for that object. As already mentioned, simply do f.read()
without an argument when calling read()
.
edit: See other answer, also the mode is wrong
Upvotes: 0
Reputation: 223
Your problem is that you open file for writing and you try to read from it.
Your variable "f" is a file object which supports input and output operations. To read the contents of the file you should use the method read(). read() can have parameter which need to be integer. The integer parameter will tell the method read how much data will it return to a variable e.g
file = open(<dir_to_some_file>, 'r')
text = read(25)
The variable text will contain 25 characters from the file content.
If you need to read all the contents of the file just do not pass any parameter to the read method e.g.
file = open(<dir_to_some_file>, 'r')
text = read()
To write in file:
text_to_file = 'This will be written to file'
file = open(<dir_to_some_file>, 'w')
file.write(text_to_file)
Use this for reference: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
Upvotes: 1