+ 2

Writing Files

Is there any way to save the file's existing content, when the file is opened in write mode?

2nd Feb 2018, 9:26 PM
Haider Ali
Haider Ali - avatar
4 Answers
+ 6
Later on, open it in the 'append' mode. open(file_name, 'a')
3rd Feb 2018, 12:14 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 5
You just close() it. Then it's saved.
2nd Feb 2018, 11:32 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 2
If you don't want to overwrite the file, do NOT open it in write mode. You can use the "r+" mode or the "a" (append) mode instead.
3rd Feb 2018, 3:33 AM
Pedro Demingos
Pedro Demingos - avatar
+ 1
when the file is opened in write mode again, file contents are removed. How to avoid this?
2nd Feb 2018, 11:47 PM
Haider Ali
Haider Ali - avatar