Question
(Python) (e.1) Copy this code into your lab7.py file on your own system (or, temporarily, into a separate file if that makes it easier for
(Python)
(e.1) Copy this code into your lab7.py file on your own system (or, temporarily, into a separate file if that makes it easier for you to experiment, but this code needs to be in yourlab7.py file when you submit it). Package it into a function called copy_file that takes no parameters and returns no value (because it does all its work by prompting the user and reading and writing files). Test it out by copying a short text file.
Then download the Project Gutenberg version of The Adventures of Sherlock Holmes from http://www.gutenberg.org/cache/epub/1661/pg1661.txt (Links to an external site.)Links to an external site. (Project Gutenberg is a wonderful resource for non-copyright-protected texts.) Call your file-copying function to make a copy of this file. [Some problems have been reported with reading Project Gutenberg files. If you run into messages saying that Python can't decode a character, open the file with open(infile_name, 'r', errors='ignore').]
(e.2) Modify your copy_file function to take one parameter, a string. If the parameter is 'line numbers', the copied file includes line numbers at the start of each line :
1: Project Gutenberg's The Adventures of Sherlock Holmes, by Arthur Conan Doyle
2:
3: This eBook is for the use of anyone anywhere at no cost and with
...
13052: subscribe to our email newsletter to hear about new eBooks.
If the parameter is anything else, the function just copies the file as before.
Note that the line number is formatted and right-justified in a five-character field. You did this task last week, so you should be able to reuse most of last week's solution.
(e.3) If you examine the file from Project Gutenberg, you see that it contains some "housekeeping" information at the beginning and at the end. You'll also see that the text itself starts after a line beginning with "*** START" and ends just before a line beginning with *"*** END". Modify your copy_file function so that if its parameter is 'Gutenberg trim' it will copy only the body of a Project Gutenberg file, omitting the "housekeeping" material at the front and end. (You may assumeyou don't have to checkthat if this parameter is specified, there will be a "*** START" line and an "*** END" line in the file.)
For this problem, you might find it more convenient to read the entire file into memory at onceperhaps into a list of linesbut it isn't strictly necessary.
(e.4) Modify your copy_file function so that if its parameter is 'statistics' it will copy the file as before but also provide these statistics (which should be familiar) about the text in the file, following the formatting shown:
16824 lines in the file
483 empty lines
53.7 average characters per line
65.9 average characters per non-empty line
Your program should provide these statistics in two places: written at the end of the copied file and printed to the Python console using print(). Try to do both of these tasks without duplicate code. That is, don't do the same calculation two times or have two copies of the same constants (e.g., "lines in the file"). [Hint: The format() method returns a string that may be saved and used in two places; the same is true for the value of an f-string.]
(e.5) As you develop your copy_file function, run whatever tests assure you that the function works correctly. But in the version you include in the lab7.py file, include only the code in part (e) above, your function definition(s), and these three calls: copy_file('line numbers') copy_file('gutenberg trim') copy_file('statistics')
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