Question: Exercise 4: Program exercise for read and readline function Write a complete Python program including minimal comments (file name, your name, and problem description) that
Exercise 4: Program exercise for read and readline function
Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function:
Problem Specification:
Read a file name which contains the values to read.
Read the contents using the read function and print the contents
Close the file
Reopen the file
Using a loop, count and print the contents of the file with a line number as shown below
Make sure you have a main function and there should not be any global variable used.
Create 2 files as shown below and test your program.
SAMPLE RUN: Enter the name of the file to open for read: data1.txt The contents of the data file data1.txt are listed below: This is a data file When you process your data file remember to strip the end of the line character. rstrip method is a string method that strips a character from end of the string line1 = line1.rstrip(' ') To handle any potential error use try/except/else closing data1.txt ================================ Reopening data1.txt line 1 This is a data file line 2 line 3 When you process your data file remember to strip the end of the line character. line 4 rstrip method is a string method that strips a character from end of the string line 5 line 6 line 7 line1 = line1.rstrip(' ') line 8 line 9 To handle any potential error use try/except/else there are 9 lines in file data1.txtSAMPLE RUN 2: Enter the name of the file to open for read: data2.txt The contents of the data file data2.txt are listed below: first second third forth and fifth closing data2.txt ================================ Reopening data2.txt line 1 first line 2 second line 3 third line 4 forth line 5 and fifth there are 5 lines in file data2.txt

Exercise 4: Program exercise for read and readline function Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the folowing problem with the main function: Problem Specification: 1. Read a file name which contains the values to read. 2. Read the contents using the read function and print the contents 3. Close the file 4. Reopen the file 5. Using a loop, count and print the contents of the file with a line number as shown below 6. Make sure you have a main function and there should not be any global variable used. 7. Create 2 files as shown below and test your program. SAMPLE RUN: Enter the name of the file to open for read: datal.txt The contents of the data file datal.txt are 1isted below: This a data file When you process your data file remember to strip the end of the line character rstrip method is a string method that strips a character from end of the string linel linel.rstrip( ') To handle any potential error use try/except/else closing datal.txt Reopening datal.txt line 1 This is a data file line 2 line 3 When you process your data file remember to strip the end of the line character line 4 rstrip method is a string method that strips a character from end of the string ine line 6 line 7 linelline1.rstrip('n') line 8 line 9 To handle any potential error use try/except/else there are 9 lines in file datal.txt SAMPLE RUN 2: Enter the name of the file to open for read: data2.txt The contents of the data file data2.txt are listed below: first second third forth and fifth closing data2.txt Reopening data2.txt line 1 first line 2 second line 3 third line 4 forth line 5 and fifth there are 5 lines in file data2.txt
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
