how to write in a document | Sololearn: Learn to code for FREE!


how to write in a document

10/27/2018 7:59:46 PM


4 Answers

New Answer


file=open("your_file.txt","w") file.write("whatever you want") file.close() #note it will delete whatever was previously in the file


Be careful when you use "w" mode because it erases everything in the file


also look at the with statement. When using a file sometimes you will forget to close it and resource management gets sloppy. The with statement will take care of this and so Ahri Fox example would be with open("your_file.txt", 'w') as my_f: my_f.write("your data") this will close when the program leaves the context of the current operation and you won't need to use try and finally. If you forget close() no worries it is what with does. No leak of you descriptor.

+2 Cant get the Link Srry Or: file = open(“I_am_a_file”, “w”) file.write(“Hello, Files!”) file.close