Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Chapter 6 - Files and Files and Exceptions Complete the following programming exercises adapted from those found at the end of Chapter 6: But
Chapter 6 - Files and Files and Exceptions Complete the following programming exercises adapted from those found at the end of Chapter 6: But note - I have made significant modifications and added steps to these problems that must be completed to receive full credit. Assignment Objectives In addition to the objectives covered in the previous chapter, you must include the following techniques, as demonstrated in your current textbook, to receive full credit for the assignment objectives portion of the Code Grading Rubric: Your program MUST USE the main function, which must be called correctly (use the template). Additionally, you must include docstrings as shown in the template. File Input File Output Opening Files Closing Files Using Loops to Process Files Processing Records Exceptions 1. File Head Display (adapted from #2) - Save the file as ch6_ex1.py Write a program that asks the user for the name of a file. The program should display only the first five lines of the file's contents. The file should show its entire contents if it contains less than five lines. Test the program using the following text files: 6-1-lines1.txt E 6-1-lines2.txt Your program must: Open file designated by user input for reading Use a loop to read no more than five lines from the file Strip the trailing from each line. (See: Reading a String and Stripping the Newline from It on page 315) Otherwise, your output will be double-spaced. Be able to handle files that have any number of lines (more or less than five lines - neither test file has exactly five lines) Close the file The output should look like this for 6-1-lines1.txt: Enter the name of the file: 6-1-lines1.txt Line 01 Line 02 Line 03 Line 04 Line 05 The output should look like this for 6-1-lines2.txt: Enter the name of the file: 6-1-lines2.txt This file only has two lines 6-1-lines1.txt Download 6-1-lines1.txt (70 Bytes) Line 01 Line 02 Line 03 Line 04 Line 05 Line 06 Line 07 Line 08 6-1-lines2.txt Download 6-1-lines2.txt (29 Bytes) This file only has two lines
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started