<jupyter_start><jupyter_text>[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/bwrsandman/learningpython/master?filepath=08-Reading-And-Writing-Files.ipynb) Reading and Writing Files  ReviewReading and writing is sometimes called io. In the world of computer science and especially in linux system, may concepts are seen as reading and writing file type objects. For example, you can read a byte from a file on a hard drive in the same way you can read a byte from a microphone. You can write a byte to the hard disk in the same way you can write a byte to the speakers to produce a frequency. The same is true for networks, usb, motors, etc.The main functions we are going to use are the `open`, `read` and `write` functions.The files used can be found in Home and jupyter can open them.When it comes to reading and writing, the extensions (e.g. ".txt") don't matter. Their use is mostly that programs like extensions because it tells them how they should open a file.Since IO is all about programming how to interpret the bytes, there is no need to worry about extensions in these excercises. That being said, we will work with ".txt" files since they are what jupyter prefers.  Reading ExerciseIn your own words explain what is happening in the following code snippets.For each line, what does python do? Where are the files, what is being read or writen and how many bytes are being exchanged?  1. Simple file readPrediction:(Double click here and enter your prediction before running the next cell)<jupyter_code>print(open("read_write_1.txt").read())<jupyter_output><empty_output><jupyter_text>2. File read by linePrediction:(Double click here and enter your prediction before running the next cell)<jupyter_code>print(open("read_write_2.txt").readlines())<jupyter_output><empty_output><jupyter_text>3. File write by linePrediction:(Double click here and enter your prediction before running the next cell)<jupyter_code>open("read_write_3.txt", "w").write("""Hello World!
- From reading exercise 3
This is a file we have created ourselves!
Go see in Home that the file now exists and has this content
""")

print(open("read_write_3.txt").read())<jupyter_output><empty_output><jupyter_text>4. Using the with statementPrediction:(Double click here and enter your prediction before running the next cell)<jupyter_code>with open("read_write_4.txt", "w") as file:
    file.write("Hello World!\n")
    file.write("- From reading exercise 4\n")
    file.write("This is a file we have created using a with statement\n")
    file.write("with statements are safer because they clean-up \n")
    file.write("and closes the file if an error occurs in the body\n")

print(open("read_write_4.txt").read())<jupyter_output><empty_output><jupyter_text>5. Write appendPrediction:(Double click here and enter your prediction before running the next cell)<jupyter_code>open("read_write_5.txt", "w").write("Hello World!\n")
print(open("read_write_5.txt").read())
print("---")
open("read_write_5.txt", "w").write("Goodbye World!\n")
print(open("read_write_5.txt").read())
print("---")
open("read_write_5.txt", "w").write("Hello World!\n")
print(open("read_write_5.txt").read())
print("---")
open("read_write_5.txt", "a").write("Goodbye World!\n")
print(open("read_write_5.txt").read())<jupyter_output><empty_output><jupyter_text>Writing ExerciseIn the following cells, write code which implements what the comments ask.Re-read the code in the same way as the Reading exerciseto catch any errors and to predict how the code will be executed.Run the code by pressing the run button from the toolbar whilethe cell is selected or go to (Cell|Run Cells) with the cellselected.<jupyter_code># 1.
# Open, read and print the contents of read_write_6.txt
# 2.
# Open and read the contents of read_write_7.txt and open and write a copy to read_write_8.txt
# 3.
# Create a function which takes a number as a parameter and using range
# write numbers counting up on each line of read_write_9.txt: 0, 1, 2, 3, ..., number<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup