Python Forum
Need Help: FileNotFoundError:[Errno 2] No such file or directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help: FileNotFoundError:[Errno 2] No such file or directory
#1
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

Attached Files

Thumbnail(s)
       
Reply
#2
Did you run the script in the directory

cd c:\Users\tiger\Downloads\
Reply
#3
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\
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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
Reply
#6
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.

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 263 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,572 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,341 Jun-27-2023, 01:17 PM
Last Post: diver999
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 998 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,124 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,547 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 1,029 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 1,655 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,372 May-17-2022, 07:49 AM
Last Post: Pedroski55
  How to read python shortcut target profile directory of Chrome Ink file sunny9495 1 1,664 Apr-12-2022, 06:12 PM
Last Post: sunny9495

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020