Part 1 45 points head-tail = 10 pts, splits 20 pts, and wc = 15 pts. Write a com
ID: 3877173 • Letter: P
Question
Part 1 45 points head-tail = 10 pts, splits 20 pts, and wc = 15 pts. 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 (including none), 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 (opening it for the program wc more than once will you cost ALL for this section!). list the first 10 lines (default) and the last 10 lines (default) in order of the given text file head_tail flag: output # lines instead of default lines Comment If the file has less than 20 lines (default), output the entire file split flags split a text file into smaller pieces put # lines of text into each output file (default= 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 Comment: Output the name of the file as it is being created WC count characters, lines, and words in a text file output order: lines words charactersExplanation / 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