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
  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a program in C++ that prompts the user to input the name of a text...

    Write a program in C++ that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a

  • Write a program that allows the user to navigate the lines of text in a file....

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...

    ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file. Enter the file name: messages.txt Output: encoded words are written to file: messages.txt.swt The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a Python program which prompts the user for a file name then a sentence. Display...

    Write a Python program which prompts the user for a file name then a sentence. Display the number of characters in the sentence and save the sentence to the file name. If the file already exists, ask the user if the file should be overwritten.

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