+ 17

why can't I print the xml-data, which is written into a file, in plain text???

I have the code https://code.sololearn.com/c3UT506idZ0Z/?ref=app and I want to print the generated xml-data, which is saved as a file, as plain text. But why do I not get an output? And how to solve the problem? What am I doing wrong?

6th Oct 2020, 10:35 PM
Jan Markus
4 Antworten
+ 5
Jan Markus as you have not closed the file before opening it again so the second time it is opening a new file with the same name. So as Ruba Kh already said, closing the file before opening it again will fix the issue👇 https://code.sololearn.com/c1efkHwY0n0M/?ref=app
7th Oct 2020, 12:31 AM
Arsenic
Arsenic - avatar
+ 12
Ruba Kh Arsenic Guys, thank you very much. 🙂👍👍👍 I know where I did make the mistake: I am very often used to use the context-manager with open('fname', 'mode') as file: # do something # do even more Special feature of the context manager is, that you don't have to worry about closing the file, because it closes the file automatically when having finished, even in an error-cased exception. But my mistake was, that in this case there is no context manager involved, and I lost sight of closing the file. 😳 I should not code in bed on the phone at 1 AM at night... 😁😁😁
7th Oct 2020, 3:34 AM
Jan Markus
+ 7
That's OK Jan Markus 😌 I can't concentrate at 1 AM either.
8th Oct 2020, 11:58 AM
Dual Core
Dual Core - avatar
+ 6
try closing the file after writing before openning it again myfile.close()
6th Oct 2020, 10:59 PM
Ruba Kh
Ruba Kh - avatar