I want to create a file and add contents to it in python.i tried it but couldn't get answer .can you please correct my error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to create a file and add contents to it in python.i tried it but couldn't get answer .can you please correct my error

filea= open(“testfile.txt”,”w”) filea.write(“Hello World”) file.close()

5th Dec 2018, 1:48 PM
Mara
6 Answers
+ 2
In first line you create filea variable and in last line you are closing file.close() here you need to write filea.close() and remember that try this code on pc this is not work on mobile.
5th Dec 2018, 2:53 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Maninder $ingh wrote << this code (...) is not work on mobile >> Did you mean << in sololearn code playground >> ? In fact, it works... since you read the file in the same code running instance, both on mobile and on browser version of SL code playground ;) Obviously, the created file(s) are just temporary and not available when you run the code again ^^ (and my edit was long time before your answer)
5th Dec 2018, 2:58 PM
visph
visph - avatar
+ 2
visph i know but this code is just working temporary but actually which file you are creating is not present in your mobile.so work on pc because on pc you can actually edit or create a files.
5th Dec 2018, 3:06 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Maninder $ingh : Sure, if the purpose is to create a file on the device used, sololearn code playground is not the good way (purpose of SL code playground is mainly testing and/or sharing code ;))
5th Dec 2018, 3:11 PM
visph
visph - avatar
+ 2
visph right sololearn playground only for beginners just for practice.if you are advance programmer or working with big projects than use real ide for coding.
5th Dec 2018, 3:13 PM
Maninder $ingh
Maninder $ingh - avatar
0
# You don't use correct quote char (probably because you're typed them in a word processor software instead of a plain text editor)... # Below code works fine: filea = open("testfile.txt","w") filea.write("Hello World") filea.close() file = open("testfile.txt") print(file.read()) file.close() # edit: I forgot to say that you misspelled your last line (file.close() instead of fila.close())
5th Dec 2018, 2:12 PM
visph
visph - avatar