Python Forum

Full Version: writing to a file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Have been trying to learn how to write to a file in Python 3, but i can't seem to get it right. I have created a continents.txt file and a continents.py file both of which are stored in the same directory.

def Main():
    continents = ["African", "Antarctic", "Asian", "Australian", European", "North_American", "South_American"]

with open("continents.txt", 'w') as f:
    for continent in continents:
    f.write(continent + "\n")

if __name__ == "__main__":
    Main()
Could someone please tell this old fool where i am going wrong please.
continents = ["African", "Antarctic", "Asian", "Australian","European", "North_American", "South_American"]
def Main():
    with open("continents.txt", 'w') as f:
        for continent in continents:
            f.write(continent + "\n")
 
if __name__ == "__main__":
    Main()
lines 4-5-6 should be indented to the right to be part of Main() function. Then you need to fix their indentation too
Also you have missing open quotes for Europe
I didn't run your code, but just from the code highlighting it is visible that you missed a first double quote (") in "European" (continents list).
Second and third problems are both indentation related. with open() block of code is not inside Main() function, which I believe is what you wanted to do. So now Main() function just assigns the list of continents to continents variable, and that's it.
Then the f.write is not part of the for loop, because it is not indented inside it. Thus f.write runs only once, and it doesn't know what continent is.
Seems like i have been running around in circles for the last couple of days trying and failing to write text to a .txt file.
Perhaps i better start at the beginning so that everyone can see exactly what i've been doing and where i am going wrong.

I created an ordinary .txt file called emptyScriptFile.txt and saved it in the folder where i save all my .py script files.

I then created and saved a file called mainScript.py in the same folder as the emptyScriptFile.txt file which has the following contents:

#!/usr/bin/env python3
text = 'Some simple text to save\nThis is the Second line.'

myFile = open('emptyScriptFile.txt', 'w')
    myFile.write(text)
    myFile.close()
Could someone show me what i've done wrong please
in this case lines 5-6 should not be indented. You will get indentation error from this code. But it's better to use with context manager when open the file, like you did before. So, this code should be

text = '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.