Question: L 0 7 Tasks L 0 7 - T 1 : Reading & Writing text files L 0 7 - T 2 : Copying from

L07 Tasks
L07-T1: Reading & Writing text files
L07-T2: Copying from one file to another
L07-T3: Testing data inside a file, palindromes
L07-T4: CSV file
L07-T5: Menu-based program - calculator. Continues from L05-T5
Submission:
Submit all the exercises to CodeGrade via Moodle before the deadline.
Note:
(Important for this week!) When opening and closing files, please use the method that has been taught in this course. If you use other methods to handle these files, CodeGrade will reject your work. The file-handling method can be found in the learning material for this week (link).
If you see some "Code Structure Tests" hidden in CodeGrade but they are not mentioned in the task description, you don't need to worry about them. They are just there to make sure the code is ok.
Be especially careful with spaces so that your output follows the sample output. Note that also each input-string in the program is ending with ' n '. The reason for this is that it makes the output in CodeGrade more readable.
L07-T1: Reading & Writing text files
Let's practice writing and reading files in functions.
Write a program consisting of two subprograms in the style of a menu-based program from previous exercises. The functions needed are file_write(name) and file_read(name).
In the main program, ask for the name of the file to be saved and pass it as a parameter to both subprograms.
file_write (name) function should ask the user for the names and save them in a text file:
a. Ask for the name in repeating structure and stop asking when the user inputs 0.
b. The file must be opened at the beginning of the function and any previous contents of the file must be destroyed upon opening.c. At the end of the function, the file must be closed.
4. file_read (name) function is for verification that the file has the desired content.
a. Open the file, read its contents, and print one name per line on the screen and finally close the file.
Note:
Please note that in this task the repetition structure (L04), subroutines (L05) and file processing (L07) must be implemented correctly. An error in any of these points will prevent the program from working properly, so check these structures from previous weeks as needed.
Make sure that your code follows the instructions, "Code structure tests" will be used to make sure that those files are handled correctly (open, handle, and close the file).
Example run 1:L07-T2: Copying from one file to anotherWrite a function file_copy (fileA, fileB) that copies the content of one text file to another. In Moodle, you can find the file "Exercise2.txt" which produces the following result:Example run 1:L07-T3: Testing data inside a file, palindromes
This task continues the earlier task L03-T5. This time the strings to be tested are read from the file and found palindromes are written to another file.
At the beginning of the main program, ask for the name of the text file to be tested. You must specify the name of the output file as Palindromes.txt. Open both files at the start. Then, read the test file one line at a time and perform the palindrome test.
Your program should print the content of each line and the result of the test to the screen: "XXX is a palindrome.", "XXX is not a palindrome." according to the example run below.
The actual test material is in the file Exercise3.txt. Some of its lines are palindromes and some are not.
Some hints to solve the task:
Note that the readline() function also returns a newline in the string, i.e. "monkey" is read from the file as the string "monkey ??? n".
The newline character should not be considered in palindrome testing. The line containing only the newline character stops reading the file (len(row)==0).
The texts may contain uppercase and lowercase letters. You should compare lowercase words. Use lower operation:The file to be read is UTF-8 encoded.
Example run 1:row 'AppleMaple' is not a palindrome.
row 'WasItACarOrACatISaw' is a palindrome.
row 'YoBananaBoy' is a palindrome.
Then the output file Palindromes.txt will look like this:
L07-T4: CSV file
Write a program that reads a CSV (Comma-Separated Values) file and prints only the second column.
In Moodle, you can find the file Exercise4.csv
Example run 1:L07-T5: Menu-based program - calculator. Continues from L05-T5
This task modifies the previously made calculator, where the selection structure of the menubased calculator was made in task L03-T3, the repetition structure L04-T5 and the subroutine structure L05-T5.
This time the task is to add file processing: read inputs from one file and write the results to another file. As a starting point for this task, you should use the program L05-T5.
This time, you only need to change the main program and the subprogram that handles reading the numbers.
Change the main program to ask for the name of the file to be read and open it for reading.
After that, open the file Exercise5_output.txt to write the res
L 0 7 Tasks L 0 7 - T 1 : Reading & Writing text

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!