how to write in a document | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

how to write in a document

27th Oct 2018, 7:59 PM
filament
4 ответов
+ 8
file=open("your_file.txt","w") file.write("whatever you want") file.close() #note it will delete whatever was previously in the file
27th Oct 2018, 8:07 PM
Ahri Fox
Ahri Fox - avatar
+ 7
Be careful when you use "w" mode because it erases everything in the file
27th Oct 2018, 9:25 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
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.
27th Oct 2018, 8:37 PM
typhon humanoïde
typhon humanoïde - avatar
+ 2
https://www.sololearn.com/Course/JUMP_LINK__&&__python__&&__JUMP_LINK Cant get the Link Srry Or: file = open(“I_am_a_file”, “w”) file.write(“Hello, Files!”) file.close
31st Oct 2018, 2:33 AM
Potato Hacker
Potato Hacker - avatar