Question

Exercise 3.1 (word count.py). Write a program that takes in a filename and string as input. Then print how many times that string appears inside the chosen file. If the file does not exist, continue asking for a filename until one is given that exists. Use your source code file as test input. Make sure to test files with that contain the same word multiple times. ? $ python3 word-count.py 2 Please enter a filename: wordcount.py 3 Please enter a string to search for: print The string print appears 102 times in the file word count.py
0 0
Add a comment Improve this question Transcribed image text
Answer #1

try:

print("Please enter a file name: ")

s=input()

print("Please enter a string to search for: ")

s1=input()

f=open(s,'r')

a=f.read().count(s1)

print("The string '",s1,"' appears ",a," times in the file '",s,"'")

except Exception as e:

print("No file found")

#this program checks for file to be read exists or not, if exists prints the number of occurances of string to be

# searched else prints "no file found"

Add a comment
Know the answer?
Add Answer to:
Exercise 3.1 (word count.py). Write a program that takes in a filename and string as input....
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
  • In Python: LoadFile is a function that takes in a string (a filename) and then returns...

    In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Write a recursive function that takes a string as input. This recursive function should open a...

    Write a recursive function that takes a string as input. This recursive function should open a file by the name of the string, and read the first word. Then attempt to open a file by the word you just read and repeat this process. This ends, when there is no file with the name you are looking for. At this point, cout the last word in the current file (the one where the first word was not a different file)....

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • You are going to write an object that takes a word (or short phrase) as an...

    You are going to write an object that takes a word (or short phrase) as an input for the Constructor and then will reverse the letters in the word. The object to implement this is referred to as **ReverseWord** and consists of **only** the following public methods: **public void setWord(String word)** - Sets the word to be processed. **public String getWord()** - Returns the word that is set with **setWord**. Note that if this is called before setWord then an...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • in c prog only please!! Name this program one.c - This program takes two command line...

    in c prog only please!! Name this program one.c - This program takes two command line arguments: 1. an input filename 2. a threshold Your program will create two files: 1. even.txt - contains all integers from the input file that are even and greater than the threshold 2. odd. txt - contains all integers from the input file that are odd and greater than the threshold • The input file will exist and only contain a set of integers....

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