Question: In Python Function 1. Write a small program which does the following: (1) Open the file named ingredients . txt for reading. (see
In Python
Function 1.
Write a small program which does the following:
(1) Open the file named "ingredients.txt" for reading. (see content below, manually create this file in your project directory)
(2) Print the contents of the file to the screen.
(3) Close the file.
chicken
curry
coconut milk
carrot
onion
potato
rice
Function 2.
Write a small program which does the following:
(1) Open the file named "attendance.txt" for appending. (see contents below, manually create this file in your project directory)
(2) Append the following lines to the file (one per line):
Jimmy Jimmerson 2023-01-05 Present
Alanis Morissette 2023-01-05 Present
(3) Close the file.
(4) Re-Open the same file for reading.
(5) Print the contents of the file to the screen (proving you appended and didn't just write)
(6) Close the file.
# attendance.txt
Kana Tanaka 2023-01-05 Present
James Brown 2023-01-05 Absent
Mary Wong 2023-01-05 Present
Function 3.
Write a small program which does the following:
(1) Try to open a file for reading named "doesnotexist.xyz" (if, for some reason, that file exists in the same directory as your program, rename it or move it)
(2) When your program crashes trying to open the non-existent file, catch the exception in a try/except block
(3) Print the following message to the screen in the exception handling block: "Error opening file, closing program."
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
