Aug-23-2018, 01:48 PM
in this case lines 5-6 should not be indented. You will get indentation error from this code. But it's better to use
also, my_file.write() will not add new line at the end of your text.
with
context manager when open the file, like you did before. So, this code should betext = 'Some simple text to save\nThis is the Second line.' myFile = open('emptyScriptFile.txt', 'w') myFile.write(text) myFile.close()but better
text = 'Some simple text to save\nThis is the Second line.' with open('emptyScriptFile.txt', 'w') as my_file: my_file.write(text)note that with both snippets the file will be saved in the current working directory, i.e. the one from which you execute the script.
Quote:I created an ordinary .txt file called emptyScriptFile.txt and saved it in the folder where i save all my .py script files.Also, if you want to append at the end of already existing file, you should open the file in 'a' i.e. append mode, not 'w'
also, my_file.write() will not add new line at the end of your text.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs