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

write a complete python program with prompts for middle = 10 points, tail-head =

ID: 3741962 • Letter: W

Question

write a complete python program with prompts for middle = 10 points, tail-head = 10 points, splits 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 program 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! 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 middle change the number of lines to skip at the top and bottom flag: Comment If the file has less than 2 times the number of lines to skip, output this message instead of the file "Insufficient lines in file list the last 10 lines (default count of lines) first and the first 10 lines (default count of lines) second in order of the given text file tail_head output # lines instead of 10 (default) lines flag: Comment: If the file has less than 2 times the requested number of lines, output the entire file in the order requested split a text file into smaller files (may be useful with the FTP Project) split flags: put # lines of text into each output file (defaults 1000 lines) input: file name file of newfiles (example: name-aa.ext, name-ab.ext, etc.) name may be blank file to be split (note extension and use it on split files) 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 the screen on one output line only output order: lines words characters

Explanation / Answer

Answer:-   Python Code : -

import sys

from itertools import islice

from itertools import zip_longest

# Function to split file into smaller files

def split(a, iterable, fillvalue=None):

args = [iter(iterable)] * a

return zip_longest(fillvalue=fillvalue, *args)

#initialising variables for WC

num_lines = 0

num_words = 0

num_chars = 0

# HEAD_TAIL printing

print(" HEAD_TAIL ")

filename = input("Enter name of file: ")

# check for file availability

try:

fptr = open(filename)

fptr.close()

except FileNotFoundError:

sys.exit("File does not exist")

# calculate number of lines, words and characters in a single loop for reusability

with open(filename) as fptr:

for line in fptr:

words = line.split()

num_lines += 1

num_words += len(words)

num_chars += len(line)

# taking input for flag

try:

flag = int(input("Enter flag number (0 for default): "))

if flag < 0:

sys.exit("Entered flag should be >= 0")

except ValueError:

sys.exit("Entered flag is not an integer")

# checking for default flag

if flag == 0:

flag = 10

# printing whole file if file size is lesser than flag*2

if num_lines < (flag * 2):

with open(filename) as fptr:

print ("printing whole file")

print (fptr.read())

# printing only head and tail based on flag size

else:

with open(filename) as fptr:

print("HEAD")

for num, line in enumerate(fptr):

if (num > (flag - 1)):

break

print (line[:-1])

print("TAIL")

for num, line in enumerate(fptr):

if (num + (flag + 1) < num_lines - flag):

continue

print (line[:-1])

# wc

print(" WC ")

# printing WC

print(str(num_lines) + " " + str(num_words) + " " + str(num_chars))

# split

print(" SPLIT ")

# taking input file name

filename = input("Enter name of file to be split: ")

# check for file availability

try:

fptr = open(filename)

fptr.close()

except FileNotFoundError:

sys.exit("File does not exist")

# read flag input

try:

flag = int(input("Enter flag number (0 for default): "))

if flag < 0:

sys.exit("Entered flag should be >= 0")

except ValueError:

sys.exit("Entered flag is not an integer")

# check for default flag

if flag == 0:

flag = 1000

# taking new files name as input

filename_new = input("Enter name of smaller files: ")

temp = 0

# checking for blank name

if filename_new == "":

filename_new = "x"

# opening file and splitting

with open(filename) as fin:

for i, lines in enumerate(split(flag, fin, fillvalue=''), 1):

with open("{0}_{1}.txt".format(filename_new,temp), 'w') as fout:

print("Writing into {0}_{1}.txt".format(filename_new,temp))

fout.writelines(lines)

temp = temp + 1