Question: 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

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:

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 files contents. If the file contains less than five lines, it should show its entire contents. Test the program using the following text files (download from BlackBoard assignment page):

6-1-lines1.txt

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

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 Databases Questions!