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.
Don’t forget to test your code as you go!
Write code that prints the entire contents of the "cities.txt" file.
Note that the expected solution has a blank line at the end because the file includes a newline after Charlotte, and print also introduces a newline after whatever is printed. For this exercise, your output should include that newline.
Write a function named peek that, when given a file name as a string, followed by an integer specifying how many lines to read, opens that file and returns a list containing the first specified number of lines in the file. If the file contains fewer than the requested number of lines, it should just include as many lines as it can. Example:
>>> peek('cities.txt', 3)
['Wilmington\n', 'Philadelphia\n', 'Boston\n']
>>> peek('oneLine.txt', 2)
['One line.\n']
Your code must close the file after it's done by using a with/as statement to open the file.
Write a function named readNumList that, when given a filename as a string, reads a list of floating-point numbers from the file. It may assume that each line of the file contains a single floating-point number, and should return all of those numbers in a list. For example:
>>> readNumList('twos.txt')
[22.0, 2.2, 0.22]
Write code (NOT a function) that writes the contents of the message variable into a file named message.txt, overwriting any old contents of that file if there were any. (You should copy the message variable definition below into your Thonny file.)
message = "I am trapped in a file!\n"
Write a function named storeNumbers that takes two arguments: a list of numbers, and a filename (as a string). It should write the numbers from the list into the file, each on a separate line.
For example, after the call storeNumbers([1, 2], 'oneTwo.txt'), the contents of the file 'oneTwo.txt' would be the string '1\n2\n'. You should (as this example demonstrates) include a newline after the last number.
Note that some of these remaining tasks may involve f-strings or try/except, which are covered in the extra practice notebook.
Write a function named extractNums that takes a file name (a string) as its only argument, and attempts to convert each line into a floating point number. It should return a list of all of the floating point numbers it is able to convert, and should print "could not convert line" every time it encounters a line that cannot be converted to a floating point number. For example:
>>> extractNums('twos.txt')
[22.0, 2.2, 0.22]
>>> extractNums('someNums.txt')
could not convert line
could not convert line
[2, 3.4]
Your code must use a try/except statement, and you must specify an exception type for the except clause.
Write a function named copyAfter that takes two filenames as arguments. It must read the first file, and then write a copy of that file to the end of the second file, preserving any text that's already there (you will need to use the 'append' mode for open to do this). For example, assuming the file twoLines.txt is initially empty:
>>> copyAfter('oneLine.txt', 'twoLines.txt')
>>> copyAfter('oneLine.txt', 'twoLines.txt')
>>> with open('twoLines.txt', 'r') as fileInput:
... print(fileInput.read())
One line.
One line.
>>>
You must use with ... as exactly twice: once for each file.
Define a function named wrapFile that takes a filename as an argument. It should read all of the lines in the file, and then overwrite the file with the new lines starting from the second and putting the first at the end. You may assume that each line of the file ends in a newline, including the last line. So for example, if a file called 'letters.txt' contained the following text:
def
ghi
abc
after calling wrapFile('letters.txt') the contents would be:
ghi
abc
def
You must use with ... as when opening the file, and you will need to do this exactly twice.