0
how to write in a document
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
+ 7
Be careful when you use "w" mode because it erases everything in the file
+ 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.
+ 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