These exercises give you more practice with reading & writing files.
The files you need for this this task can be found here. Write your code in a new python file that you save in that folder. Feel free to comment out each task after you finish it to simplify your printed output.
Don’t forget to test your code as you go!
Write code (NOT a function) that reads the file 'animals.txt' and stores the first line as a string in the variable fluffy. Use with ... as when opening the file.
Write code (NOT a function) that writes the words 'hello' and 'world' on the first two lines of the file 'hello.txt', overwriting whatever used to be in that file. Your code should include a newline after the 'd' of 'world'.
You must use with ... as to open the file.
Define a function named readLast that takes a filename as its only argument. It should read the file and return the last line of that file as a string.
For example:
>>> print(readLast('cities.txt'))
Charlotte
>>> print(readLast('someNums.txt'))
five
Note that you will have to read and discard all of the lines of the file except the last one (there is no way to read just the last line based on what we've taught you). Also for this exercise you will have to use with ... as. You may assume that the file contains at least one line.
Define a function named listLines that accepts a filename (a string) as its only argument and prints out the contents of that file, with a line number before every line (separated from the line by a space).
For example:
>>> listLines('cities.txt')
1 Wilmington
2 Philadelphia
3 Boston
4 Charlotte
>>> listLines('someNums.txt')
1 one
2 2
3 3.4
4 five
Note that line numbers start from 1. You must use with ... as when opening the file. It's okay if your output has an extra blank line at the end, but it should not have extra blank lines between the lines of the file. Remember that print automatically adds a newline to everything it prints, and lines read from files contain their own newlines. You can suppress the newline added by print by adding an extra argument end='', like in this example:
>>> print('hello\n') # with two newlines (one in string + one automatic)
hello
>>> print('hello\n', end='') # with one newline (automatic newline suppressed)
hello
>>>
Define a function named copyFile that takes two filenames as arguments. It should copy the first file to the second file, overwriting any previous contents of that second file. You must use with ... as to open the files.
Define a function named writeReport that takes a filename and a list of tuples as arguments. It should write each tuple into the file on a separate line, with a colon and a space between the first and second parts of the tuple. It should write these lines at the end of the file, preserving any previous file contents. So for example, after calling:
>>> writeReport('farm.txt', [('sheep', 1), ('cows', 2)])
...the contents of farm.txt should be:
sheep: 1
cows: 2
...if you then called:
>>> writeReport('farm.txt', [('chickens', 3), ('pigs', 4)])
...the contents of farm.txt would now be:
sheep: 1
cows: 2
chickens: 3
pigs: 4
Note that your function should convert any kind of data to a string so that it can be written to the file (this will happen automatically in an f-string, or you can use the str function to do it manually).
As usual, you must use with ... as to open the file.
Define a function named readReport that takes a filename as an argument. It will assume that each line of the file contains a string and a floating-point number, separated by a colon and a space. It should read the file and return a list of tuples, one per line of the file, where the first item in each tuple is the string from that line (before the colon) and the second item in each tuple is the number from that line (after the colon + space). So for example, if we had used writeReport to create a file farm.txt containing:
sheep: 1
cows: 2
chickens: 3
pigs: 4
Then calling readReport we would get:
>>> readReport('farm.txt')
[('sheep', 1.0), ('cows', 2.0), ('chickens', 3.0), ('pigs', 4.0)]
Notice how the numbers are converted to floating-point numbers.
If the file does not contain a valid report (e.g., because there's a line without a colon, or because the stuff after the colon on a line can't be converted to a floating-point value) it should print the string "Invalid report file." and return None. You will want to use try/except to do this, but note that you may not use except without specifying an exception type.
Hint: You can use the index method of a string to find where a certain character appears in that string, or you can use the split method with an argument to split the string around a particular character. So 'a:b:c'.split(':') will result in the list ['a', 'b', 'c'].