Question
Write a program that prompts the user for a filename. Open the file, and read it one line at a time. Do not use hard-coded
Write a program that prompts the user for a filename. Open the file, and read it one line at a time. Do not use hard-coded file paths in your program, and do not require the user to specify the path. Instead, use the %cd magic command in your IPython console (magic commands can only by used in an IPython console; you can't use them in a .py file) to position yourself in the code3 directory. Now run your program, ask the user for the filename, and then open and read it. For each line split the line into a listof words called file_word_list. For each word in the current file_word_list, convert the word to lowercase and look to see if it is in a list called script_list (a list that is initially empty). If the word is not in script_list, add it to the script_list. Sort the script_list alphabetically.1. Within the same program define a function called freq_count(). This function accepts a str and alist of words as arguments. [Note: str is a Python object type. Hence, you should never use str asan identifier. str already refers to the string class object, and by using the name str to refer to something else, you lose the ability to reference the string class object str! Instead, you can use str as a compositional element in an identifier name, such as search_str.] I would name my arguments, search_str and word_list, respectively. But thats just because I like my variable names to tell me what they represent, and why I am using them. Note that are careful to avoid confusion by using two different names, file_word_list and word_list. These variables are created and used in different scopes, and we dont want to delude ourselves into thinking that they are the same variableso we give them different names. Your freq_count() function traverses the list of words (word_list) and searches each word and counts the occurrences of the substring (search_str) within each word. For this assignment, you are not allowed to use the string count() method or the re.findall(...) method to obtain your counts. Instead, you must use the string find(...) method. Hint: try specifying the starting position of your find operation by passing the starting position in as a variablei.e., use two arguments when you call the find(...)method.The freq_count() function should print each word along with the number of substringoccurrences found within the associated word.Please note that you are not counting the number of occurrences in the list or in the file! Your counts are counts within the wordseach unique and separate word, that is, not all of the words!2. Modify this same program so that it accepts the filename and the substring str as input from theuser. After reading the file to build and sort the script_list, pass the script_list into a call of the freq_count() function. Test your program with the romeo.txt file that comes as a text file resource with our textbook. Youll find this file in the code3 folder that you were asked to download onto your system.
Note: With step 2, you are printing each word, not just words with non-zero occurrences of the substring str. As examples of what the substring search results would be, given the word there and a substring str of th, the result would be 1, and the print result for that one word would be there 1; given the same word there and a substring str of e, the result would be 2, and the print result for that one word would be there 2; and finally, given the same word there and a substring str of z, the result would be 0, and the print result for that one word would be there 0.Here is an example run:Enter filename: romeo.txtEnter substring: oalready 0and 0arise 0breaks 0but 0east 0envious 1fair 0grief 0is 0it 0juliet 0kill 0light 0moon 2pale 0sick 0soft 1sun 0the 0through 1what 0who 1window 1with 0yonder 1The romeo.txt file is provided within the code3 directory that you should have downloaded from our textbook website (please see and read the syllabus). If you try to run your program, and
it cant find your downloaded romeo.txt file, that usually means that you did not navigate (use the %cd magic command) to code directory before running your program. The %pwd magic command will tell you what your current working directory is, and the %ls magic command will list the contents of the current working directory. To ensure that your magic commands to work in your Spyder IPython console, you need to be sure that your Spyder configuration is correct. Inorder for your magic command to "stick", do this: Tools --> Preferences --> Current working directory... click the radio button "The current working directory".By %cd-ing into the "code3" directory, you can just use the simple filename (no directory path) for your data file, and it just works. Heres the syntax:%cd
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started