Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. print('\n')
  2. print('----------Part E----------')
  3. print('\n')
  4.  
  5. def copy_file(str1:str):
  6.     infile_name = input("Please enter the name of the file to copy: ")
  7.     infile = open(infile_name, 'r')
  8.     outfile_name = input("Please enter the name of the new copy:  ")
  9.     outfile = open(outfile_name, 'w')
  10.  
  11.     if str1 == 'line numbers':
  12.         x = 1
  13.         for line in infile:
  14.             outfile.write("{0:5.0f}: {1:1}".format(x,line))            
  15.             x += 1
  16.            
  17.     if str1 == 'Gutenberg trim':
  18.        exists = False
  19.        for lines in infile:
  20.             if '*** START ' in lines:
  21.                 exists = True
  22.             if exists:
  23.                 outfile.write(lines)
  24.             if '*** END' in lines:
  25.                 exists = False
  26.                
  27.     if str1 == 'statistics':
  28.         l = 0
  29.         emptyLines = 0
  30.         counter = 0
  31.         lineCharacters = []
  32.         nonEmptyCharacters = []
  33.         book = infile.readlines()
  34.        
  35.         for lines in book:
  36.             l += 1
  37.             if lines == '\n':
  38.                 emptyLines+= 1
  39.             else:
  40.                 nonEmptyCharacters.append(len(book[counter]))
  41.             lineCharacters.append(len(book[counter]))
  42.             counter += 1
  43.         lineAverage = float((sum(lineCharacters))/l)
  44.  
  45.         if emptyLines == 0:
  46.             nonEmptyAverage = lineAverage
  47.         else:  
  48.             nonEmptyAverage = float(sum((nonEmptyCharacters))/emptyLines)
  49.  
  50.         print("{0:0}         lines in the list".format(l))
  51.         print("{0:3}       empty lines".format(emptyLines))
  52.         print("{0:7.1f}   average characters per line".format(lineAverage))
  53.         print("{0:7.1f}   average characters per non-empty line".format(nonEmptyAverage))
  54.  
  55.     else:
  56.         for line in infile:
  57.             outfile.write(line)
  58.     infile.close()
  59.     outfile.close()
  60.  
  61. copy_file('line numbers')
  62. copy_file('Gutenberg trim')
  63. copy_file('statistics')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement