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

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

  • 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 inputs a text file. The program should print the unique words in...

    Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Note: The sort function sorts first numbers, then capital letters, then lowercase letters. That is fine for this assignment as you can see from the sample output. But even if the same word appears multiple times in the file it should only be displayed once in the output. (python)

  • Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1...

    Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1c Unique Words Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and a list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not found’ error. Store each word as an element of list. Be sure not...

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
Active Questions
ADVERTISEMENT