Lab: Files#
The problems in this lab will help you understand reading and writing files.
Part 1: Reading and Writing a Text File#
This part will help you understand reading and writing.
1. Open and Read a File#
Create a file called ``test.txt`` with a few lines in it. Read and print the first line of the file. Don’t forget to close the file.
[ ]:
2. Read Two Lines#
Wirte a program that reads the first two lines of test.txt
and prints them. Don’t forget to close the file.
[ ]:
3. Write a File#
Use Python to write the following into a file called fish.txt
:
One fish,
Two fish,
Red fish,
Blue fish.
The program should create the file if it doesn’t exist.
[ ]:
4. Write Data#
Write a program that uses the widgets in the next cell to:
Get the name of a file (
widget_filename
)Get a word (
widget_word
)Get a number (
widget_times
)
[ ]:
import ipywidgets
widget_filename = ipywidgets.Text(description="Filename:")
widget_word = ipywidgets.Text(description="Word:")
widget_times = ipywidgets.IntSlider(description="Times:")
display(widget_filename, widget_word, widget_times)
Open the file widget_filename.value
then write widget_word.value
widget_times.value
number of times. Hint: Remember what happens when you multiply a string by an integer (e.g. ``”hello” * 100``)
[ ]:
5. Read Lines and Reorder#
Write a program that reads the four lines in fish.txt
and prints them in reverse order.
[ ]:
6. Read Into a List#
Write a program that reads fish.txt
into a list.
[ ]:
7. Print the Last Line#
Use the list you created in the previous question to print the last line of the file.
[ ]:
Part 2: Functions with Files#
In this part you’ll write functions that manipulate files.
1. Read a Whole File#
Write a function called read_file
that has an argument filename
that contains the name of a file. The function should read the file and return it’s entire contents. The function must close the file before returning.
Name:
read_file
Arguments:
filename
(string) The name of a file to read.
Returns: (string) The contents of the file.
[ ]:
Test your function below. You can use the file example.txt
for testing purposes.
[ ]:
2. Write a Whole File#
Write a function called write_file
that takes two arguments filename
and contents
. The function opens the file named in filename
removing any previous contents and replacing them with contents
. Return the number of characters written to the file. The function must close the file before returning.
Name:
write_file
Arguments:
filename
(string) The name of a file to write.contents
(string) The stuff to write into the file.
Returns: (integer) The number of characters written.
[ ]:
Test your function in the cell below. Write to a file named output.txt
[ ]:
3. Summing Values#
Write a function called file_sum
that takes one argument filename
, the name of a file. The function should read the first four lines of the file and convert the lines to float
, then return the sum of the numbers in the file.
Name:
file_sum
Arguments:
filename
(string) - The name of a file
Returns: (float) The sum of the first four lines in the file.
[ ]:
Test your function in the code below. The file numbers.txt
in the current directory can be used for testing.
[ ]:
4. Top to Bottom#
Write a function called top_to_bottom
that takes one argument filename
. The function reads the file and re-rewrites it with the first line of the file moved to the bottom. For example if the file looked like this before top_to_bottom
:
line 1
line 2
Mary had a little
Lamb.
And stuff
After top_to_bottom
the file should look like this:
line 2
Mary had a little
Lamb.
And stuff
line 1
Name:
top_to_bottom
Arguments:
filename
(string) - The name of a file
Returns: None
[ ]:
Test your function in the cell below:
[ ]: