0

About this below Code

Language:Python msg = "Hello world!" file = open("newfile.txt", "w") amount_written = file.write(msg) print(amount_written) file.close() Output: 12 I have a doubt here 1)Why does it print length of the string rather than printing "Hello World! "? 2)Why do we have to use another varaible "amount_written" here if we run the code like this msg = "Hello world!" file = open("newfile.txt", "w+") file.write(msg) print(file.read()) file.close() Output: No output Can anyone please Explain this ? Thank you

18th Apr 2020, 4:00 AM
Thithicksha Suresh
Thithicksha Suresh - avatar
7 Answers
18th Apr 2020, 9:28 AM
Jan Metz
Jan Metz - avatar
+ 1
You must Close the File and Open it again
18th Apr 2020, 9:31 AM
Jan Metz
Jan Metz - avatar
0
Even that will print the length
18th Apr 2020, 9:25 AM
Thithicksha Suresh
Thithicksha Suresh - avatar
0
But its "W+" it means write and read why do i need to close it again
18th Apr 2020, 10:13 AM
Thithicksha Suresh
Thithicksha Suresh - avatar
0
I don't think it was saved until it was closed
18th Apr 2020, 10:18 AM
Jan Metz
Jan Metz - avatar
0
But without using len how does the program show "12" as output
18th Apr 2020, 11:45 AM
Thithicksha Suresh
Thithicksha Suresh - avatar
- 1
Try Print(len(file.read()))
18th Apr 2020, 7:50 AM
Jan Metz
Jan Metz - avatar