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

55 points middle 10 points, tail_head 10 points, split 20 points, and mod_wc 15

ID: 3741915 • Letter: 5

Question

55 points middle 10 points, tail_head 10 points, split 20 points, and mod_wc 15 points Write a complete Python program with prompts for the user to get the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (flags allows the user to change the default conditions or to use the defaults, and for any other input that this program may need from the user: split has an option of naming the smaller files. Your progra will open the input file once only and using seek(0) to avoid re-opening the file will cost you all points for that part! middle list the middle lines of the file by skipping the first 10 lines (default) and skipping the last 10 lines (default) in order of the given text file flag: Comment: change the number of lines to skip at the top and bottom the If the file has less than 2 times the number of lines to skip, output this message instead of file "Insufficient lines in file list the last 10 lines (default count of lines) first and the first 10 lines (default cour of lines) second in order of the given text file tail_ head flag: Comment output # lines instead of 10 (default) lines If the file has less than 2 times the requested number of lines, output the entire file in the orde requested split flags: split a text file into smaller files (may be useful with the FTP Project) put # lines of text into each output file (defaults 1000 lines) input: file name file to be split (note extension and use it on split files) file of newfiles (example: name-aa.ext, name-ab.ext, etc.) name may be blank so the files are: xaa.ext, xab.ext, etc. (letters only, no numbers) Comment Output the name of the file as it is being created and filled mod_wc count and output the number of characters, lines, and words from a text file to output order: lines words characters the screen on one output line only

Explanation / Answer

Here is my solution to your problem

FileReader.py

print("Enter the file name to be opened (with extension): ")
file_name = input()
dot_index = file_name.rfind('.')
ext = file_name[dot_index:]

# Checking for existence of file to be read
try:
   file = open(file_name, 'r')

except FileNotFoundError:
   print("File does not exist")
   exit()

lines = file.readlines()       # Converts content of file into a list, where each element is a line of the file
number_of_lines = len(lines)


number_of_words = 0               # To calculate total number of words in the file
number_of_chars = 0               # To calculate total number of characters in the file

for line in lines:
   words_in_line = line.split()           # Splitting each line into a list of words
   number_of_words+=len(words_in_line)      

   for word in words_in_line:
       number_of_chars += (len(word) + (len(words_in_line)-1))       # Calculating number of characters in each word

def middle():
   flag = 10           # As defined in the problem
   print("Enter flag (press enter to use default): ")
   flag = input()

   if flag is '':
       flag = 10

   else:
       flag = int(flag)

   if number_of_lines < (2*flag):
       print("Insufficient lines in file")
       # return
  
   else:
       for i in range(flag, (number_of_lines-flag)):   # Prints lines in the middle range with a blank line in between 2 consecutive lines
           print(lines[i])


def tail_head():
   flag = 10
   print("Enter flag (press enter to use default): ")
   flag = input()

   if flag is '':
       flag = 10

   else:
       flag = int(flag)

   if number_of_lines < (2*flag):
       for x in lines:
           print(x)

   else:
       print("Tail:")
       for i in range(number_of_lines-flag, number_of_lines):       # Prints last <flag> lines
           print(lines[i])

       print()
       print("Head:")              
       for i in range(0, flag):       # Prints first <flag> lines
           print(lines[i])


def split():
   flag = 1000
   print("Enter flag (press enter to use default): ")
   flag = input()

   if flag is '':
       flag = 1000

   else:
       flag = int(flag)

   print("Enter sub_file_name (press enter to use default): ")
   name = input()

   sub_file = None
   char1 = 'a'
   char2 = 'a'
   start = 0

   for i in range(0, number_of_lines):
       if (i+1)%flag==0 and i!=0:       # Checking for places to divide file according to value of flag
           if sub_file:
               sub_file.close()
           sub_file_name = "{}-{}{}{}".format(name, char1, char2, ext)       # File name assignment
           sub_file = open(sub_file_name, 'w')
           print(sub_file_name)
           sub_file.writelines(lines[start:i+1])

           if char2 is 'z':
               char2 = 'a'
               char1 = chr(ord(char1)+1)
           else:
               char2 = chr(ord(char2)+1)

           start = i+1

   if sub_file:
       sub_file.close()

   last_file_name = "{}-{}{}{}".format(name, char1, char2, ext)   # Last file
   last_file = open(last_file_name, 'w')
   print(last_file_name)
   last_file.writelines(lines[start:number_of_lines])
   last_file.close()


def mod_wc():
   print("Lines: ", number_of_lines, ", Words: ", number_of_words, ", Characters: ", number_of_chars)


exit = 'n'
while(exit is not 'x'):           # Menu to drive the program
   print("(m)iddle / (t)ail_head / (s)plit / mod_(w)c / e(x)it: ")
   option = input()
   if option is 'm':
       middle()

   elif option is 't':
       tail_head()

   elif option is 's':
       split()

   elif option is 'w':
       mod_wc()

   elif option is 'x':
       print("Exting program...")
       file.close()
       exit = 'x'

   else:
       print("Invalid input. Try again")

Hope this helps! Please do upvote :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote