Question

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#taking the file name as input from the user
filename = input("enter name of the file:\t")
#the try block is to print the exception if the file doesn't exist
try:
   #opening the file for reading in read mode
   f = open(filename, "r")
   #it will take all the lines in the file as a list
   lines = list(f)
   #a dictionary to keep track of frequency of each word in the file
   d = {}
   #iterate over the list
   for i in lines:
       #as the element in the list is a line (means sentence) split the sentence into words
       temp = i.split()
       #for each word in the sentence add it into the dictionary
       for j in temp:
           #remove dot if it exist in the end of the word(like word.)
           j = j.rstrip(".")
           #if the word is not present in the dictionary add it to it and make it frequency as 1
           if j not in d:
               d[j] = 1
           #if the word is present in the dictionary increment its frequency
           else:
               d[j] += 1
   #sort the keys in the dictionary
   s_d = sorted(d.keys())
   #for each word in the sorted list print word and its frequency
   for i in s_d:
       print("{} {}".format(i, d[i]))
except:
   print("file doesn't exist")

If you have any doubts please comment and please don't dislike.

Activities Text Editor Wed 00:10 Open A unique.py -Desktop Save file.txt unique.py filename = input("enter name of the file: \t") try: f = open(filename, "1") lines = list(f) d = {} for i in lines: temp = i.split() for j in temp: j = j.rstrip(".") if j not in d: d[j] = 1 else: d[j] += 1 s_d = sorted(d.keys()) for i in s_d: print("{} {}".format(i, d[i])) except: print("file doesn't exist") Python Tab Width: 8 Ln 15, Col 20 INS

Activities Terminal Wed 00:10 deepika@deepika-TravelMate-P243-M: -/Desktop File Edit View Search Terminal Help deepika@deepika-TravelMate-P243-M:-/Desktop$ python3 unique.py enter name of the file: file.txt a 1 contains 1 data 1 file 1 is 1 it 1 this 1 deepika@deepika-TravelMate-P243-M:~/Desktops AU COWO

Activities Text Editor Open A Wed 00:10 file.txt /Desktop Save = 0 file.txt unique.py this is a file. it contains data. Plain Text Tab Width: 8 Ln 1, Col 34 INS

Add a comment
Know the answer?
Add Answer to:
write a python program that prompts the user for a name of a text file, opens...
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
  • 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...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    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_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Write a console program in Java that allows the user to specify a text file, opens...

    Write a console program in Java that allows the user to specify a text file, opens the file, and prints a two-column table consisting of all the words in the file together with the number of times that each word appears. Words are space-delimited and case-sensitive. The table should list the words in alphabetical order. Proper code documentation should be included in the source code.

  • 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...

  • Python 3:Write a program that inputs a text file. The program should print the unique words...

    Python 3:Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.   Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name: example.txt brown dog fox jumps lazy over quick the

  • Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You wi...

    Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You will be using the file text.txt to test your program. Print out all of the individual, unique words contained in the file, in alphabetical order Print out the number of unique words appearing in text.txt. Call your program: YourName-HwrklXa.py Make sure that your name appears as a comment at the beginning of the program as well as on the output display showing...

  • Python Programming Write a program that counts how often a word occurs in a text file....

    Python Programming Write a program that counts how often a word occurs in a text file. Input: Ask the user for the name of an ASCII text file. Output: Display the numbers of top frequently appeared words and their frequencies. Pseudocode: 1) Read the file 2) Split the file into words 3) Count each word 4) Sort the words by frequencies, starting with the most frequent ones 5) Internally you should use some sort of data structure to keep track...

  • 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.

  • IN PYTHON 3: Write a Python program to read the attached text file containing words, count...

    IN PYTHON 3: Write a Python program to read the attached text file containing words, count the occurrence of each unique word and store the words in alphabetical order in a text file listing the word and the total count of that word. You cannot use any advanced features such as dictionaries. The program requires user input to determine the name of the file. (example of the text file that needs to be read = for a brief moment I...

  • 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

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