Python Forum

Full Version: Need Help: FileNotFoundError:[Errno 2] No such file or directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I save a txt file with name 1pytest.txt and python the codes as a python file with name 1py.py in the same folder c:\Users\tiger\Downloads\

name = input('Enter file:')
handle = open(name, 'r')
counts = dict()

for line in handle:
    words = line.split()
    for word in words:
        counts[word] = counts.get(word, 0) + 1

bigcount = None
bigword = None
for word, count in list(counts.items()):
    if bigcount is None or count > bigcount:
        bigword = word
        bigcount = count

print(bigword, bigcount)
see screenshot (error message) in attachment
see screenshot (files in folder) in attachment

Can anyone tell me how to solve the problem? Thanks
Did you run the script in the directory

cd c:\Users\tiger\Downloads\
Yes, I did. Please see the 1st line in the first attached screenshot. I ran the script named "1py.py"

(Sep-11-2022, 03:29 PM)Axel_Erfurt Wrote: [ -> ]Did you run the script in the directory

cd c:\Users\tiger\Downloads\
I am quite certain that code you presented will not produce the error message you have on screenshot. handle in your code and on screenshot are different.
perfingo's right. If you ran the posted code the error message would be

Error:
Traceback (most recent call last): File "1py.py", line 1, in <module> handle = open(name, 'r') <<< This would not replace "name" with the text you entered. FileNotFoundError: [Errno 2] No such file or directory: 1pytest.txt
Heart
Thanks perfingo and deanhystad.

Sorry, I attached the incorrect screenshot in my original post. I attached the correct error message screenshot in the reply that is same as what deanphystad posted.
Thank you all. I found the root cause and fixed it. 1pytet.txt was saved as the name of the txt file.