Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON 3.4 link :https://eee.uci.edu/15m/36530/labs/lab7.pdf I need help with Pa

ID: 666571 • Letter: P

Question

PYTHON 3.4

link:https://eee.uci.edu/15m/36530/labs/lab7.pdf

I need help with Part C

Part c This Python code copies a file, line by line. It presumes that the input and output files will be in the same directory (folder) as the code itself. infile_name = input("Please enter the name of the file to copy: ") infile = open(infile_name, 'r') outfile_name = input("Please enter the name of the new copy: ") outfile = open(outfile_name, 'w') for line in infile: outfile.write(line) infile.close() outfile.close() (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). 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 (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').] (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. (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 assume—you don't have to check—that if this parameter is specified, there will be a "*** START" line and an "*** END" line in the file.) (4) Modify your copy_file function so that if its parameter is 'statistics' it will copy the file as before but also print out 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

Explanation / Answer

def copy_file(x):
   infile_name = input("Please enter the name of the file to copy: ")
   infile = open(infile_name, 'r')
   outfile_name = input("Please enter the name of the new copy: ")
   outfile = open(outfile_name, 'w')
   lineNo = 1
   total = 0
   empty = 0
   totalChar = 0
   for line in infile:
       if len(line) == 0:
           empty = empty + 1
       if x == 'line numbers':
           line = (5 - len(str(lineNo))) * ' ' + str(lineNo) + ': ' + line
           lineNo = lineNo + 1
       outfile.write(line)
       total = total + 1
       totalChar = totalChar + len(line)
   if x == 'statistics':
       print(str(total) + ' ' + 'lines in the file')
       print(str(empty) + ' ' + 'empty lines')
       print(str((totalChar * 1.0) / (total * 1.0)) + ' ' + ' average characters per line')
       print(str((totalChar * 1.0) / ((total - empty) * 1.0)) + ' ' + ' average characters per non-empty line')

   infile.close()
   outfile.close()

copy_file('statistics')