Why output do not shows in order of precedence | Sololearn: Learn to code for FREE!
0

Why output do not shows in order of precedence

file = open("newfile.txt", "w") file.write("Some new text") print(file.write(" PYTHON88")) file.close() file = open("newfile.txt", "r") print("Reading new contents") print(file.read()) print("Finished") file.close() Ouput is: 8; Reading new contents;Some new text PYTHON88; Finished. Questions: I) . Why do the output 8 occur here. Is it the length of the string PYTHON88. I) In Order of CODE, the Output "Some new text PYTHON88" should hAve occur before "Reading new contents". But how come not. II) Did it print(file.read()). If not why is it there. Why it don't gives Argument error like in print(file.write()) III) Why do Output gives Argument Error when I write use print(file.write()) . IV.) What happens if file.close() is not used. Do it make any changes. V) Did the code overwrite, which line of code shows overwrite.

15th Feb 2021, 2:19 PM
We Doru
We Doru - avatar
7 Answers
+ 3
(I) file.write() returns the number of characters written. This is where the number 8 from. (or 9 because I counted it) (second I) print("Reading new contents") is before print(file.read()), I wonder why you think file.read() would be output first? (II) Yes. And no exception is occured. I don't know what do you mean by Argument error. (III) I didn't get the error. (IV) To be honest, I have no idea. But I found a useful post you could check out: https://stackoverflow.com/questions/25070854/why-should-i-close-files-in-JUMP_LINK__&&__python__&&__JUMP_LINK (V) Yes, because it's write mode.
15th Feb 2021, 2:36 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 3
Wedo Ru You didn't print it. Try print("Some new text")
15th Feb 2021, 4:54 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
You got argument error since write() method expects atleast 1 argument.
15th Feb 2021, 2:44 PM
Abhay
Abhay - avatar
+ 1
Wedo Ru because it doesn't expects argument , you should actually read about what is arguments and parameters , not every function or method needs an argument.
15th Feb 2021, 4:42 PM
Abhay
Abhay - avatar
0
Abhay Then why print(file. read()) don't give argument error just like print(file.write())
15th Feb 2021, 4:40 PM
We Doru
We Doru - avatar
0
Abhay CarrieForle But why it didn't return number from "Some new texts" As It did to"PYTHON88"
15th Feb 2021, 4:45 PM
We Doru
We Doru - avatar
0
By using w mode, we Are overwriting.Overwriting meAns to Delete the files from files.txt and when we write file.write("Some new texts"), we Are replAcing the deleted or overwritten file with the String. Right. Also the print(file.write())function acts as Append method And len function. Isn't it.
15th Feb 2021, 5:01 PM
We Doru
We Doru - avatar