I'm trying to run the following code but I keep getting this error every time I run it:
[Errno 2] No such file or directory: 'words_sorted.txt'
This is the code I'm trying to run but can't figure out how to not get this error. I have also attached the txt. file
#function definition
def words_with_letters(words, letters):
#if letters is empty string then return words list
if letters == "":
return words
#new list for storing all the subsequences
subseq = []
#iteration over the words list
for word in words:
index = 0
#iteration over the word string
for char in word:
#letter which is initialized to the first letter of letters string
letter = letters[index]
#if letter is found in the word string
if char == letter:
#updating the letter to next letter of the letters string
index += 1
#if end of letters is reached, adding the word to subsequences
if index == len(letters):
subseq.append(word)
break
return subseq
#creating empty list for storing the words in text file
words = []
#opening the text file in the read mode
with open("words_sorted.txt") as file:
#iterating over the file object
for line in file:
#removing the newline character from the line and adding word to the words list
word = line.strip()
words.append(word)
words_sorted.txtlocated on your filesystem, relative to the script? If it's not in the same directory, why do you expect that file to be found if you're not explicitly defining its path?words_sorted.txt, and your script, are both located on the desktop? Putting theopeninside your function won't make a difference if you're not providing the right file path. Also there's no such thing as a "def function", it's just called a "function" - "def" just means "define", as in "define function"..pyfile? Unless it's also in theCCPS 109folder, then it's no wonder you're getting this error. If you tell us where the script is we can tell you what you need to do to stop the error.