Question

head, tail = 10 pts, splits 20 pts, and wc = 15 pts. Write a complete Python program with prompts for the user for 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 head_taillist the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: output # lines instead of default lines Comment If the file has less than 20 lines (default), output the entire file split a text file into smaller pieces put # lines of text into each output file (default= 1000 lines) file to be split (note extension and use it on split files) so the files are: xaa.ext, xab.ext, etc. split flags: input: file namefile of newfiles (example: name-aa.ext, name-ab.ext, etc.) name may be blank, Comment: Output the name of the file as it is being created WC output order: lines words count characters, lines, and words in a text file characters
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\n\nHEAD_TAIL\n\n")

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("\n\nWC\n\n")

# printing WC

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

# split

print("\n\nSPLIT\n\n")

# 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

Add a comment
Know the answer?
Add Answer to:
Write a complete Python program with prompts for the user for the main text file (checks...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT