Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help...using Python 3 Task 2 - Reading and Sorting a File This program requires you to read a file (using readlines()), sort the lines,

Please help...using Python 3

image text in transcribed

Task 2 - Reading and Sorting a File This program requires you to read a file (using readlines()), sort the lines, and also slice a list. This program does not require that you use functions, unless you want to. Name this file sortFile.py. Your program must read a single input, which is the name of a file to open. You should provide the following prompt: Type the name of the file: (Print a blank line after reading the value from the user. It will make it easier for us to test your code.) You are not required to perform any error checking on the file name; if the name is not a valid filename, or if the file doesn't exist, it's OK if you simply allow Python to throw an exception. However, if you know how, you are encouraged to catch the exception and print a better error message. Once you open the file, your program must perform the following steps: Read the entire contents into a list using readlines (). (There are lots of other ways to do this, but this program requires that you do it this way.) See https://docs.python.org/3/tutorial/inputoutput.html Sort the list of file lines (into a new list). See https://docs.python.org/3/library/functions.html Slice the first 10 elements from the sorted list, and print that (by passing the slice to Python's print () function). (A loop is possible here, but you are required to use slicing; slicing is much more elegant!) Print out the original list (by passing it to print ())

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions