Good work! Now it’s time to write some data to a new
We added the list comprehension from the first exercise to the code in the editor. Our goal in this exercise will be to write each element of that list to a file called
output.txt file that you write to will be created in your current folder - for simplicity, the folder has been hidden.
output.txt will list each number on its own line.
We can write to a Python file like so:
my_file.write("Data to be written")
.write() method takes a string argument, so we’ll need to do a few things here:
You must close the file. You do this simply by calling
my_file.close() (we did this for you in the last exercise). If you don’t close your file, Python won’t write to it properly. From here on out, you gotta close your files!
my_list to get each value.
my_file.write() to write each value to a text file called,
Make sure to call
str() on the iterating data so
.write() will accept it.
Make sure to add a newline (
+ "\n") after each element to ensure each will appear on its own line.
my_file.close() to close the file when you’re done.
Passing the exercise means that you successfully wrote