Python Forum
FileNotFoundError: [Errno 2] No such file or directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FileNotFoundError: [Errno 2] No such file or directory
#1
i have been getting the errno2 on my raspberry pi aswell i was following this tutroial.

https://maker.pro/raspberry-pi/projects/...en-display

can someone help me

Error:
Traceback (most recent call last): File "C:\Users\user\Documents\hi.py", line 305, in <module> window.resize_icons() File "C:\Users\user\Documents\hi.py", line 284, in resize_icons fp = open(original_path) FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Documents\\RaspberryPi-Noticeboard-master\\WeatherIconsclear-day.png'
Reply
#2
First question: does the file exist?
Reply
#3
Yes the files exist in the directory. You can also check the link I posted to the tutorial and see if you are getting the same error.
Reply
#4
Has anybody managed to use the code in the tutorial and find a fix.
Reply
#5
saqib1066 Wrote:FileNotFoundError: [Errno 2] No such file or directory:
I have never seen Python lie about this,so what reported is the problem..

To show a test with the part that give error.
First test:
import os

directory = r'C:/code/img/' # See that i use / other way
for file in os.listdir(directory):
    print(file)
Output:
foo.html img.py test.png WeatherIconsclear-day.png
Works see that files are there.
Add second part and print() work fine as debug tool,can also add repr.
import os

directory = r'C:/code/img/' # See that i use / other way
for file in os.listdir(directory):
    #print(file)
    if file.endswith('.jpeg') or file.endswith('.png'):  # expand depending on what file types your icons are
        original_path = directory + file
        # open images so they can be resized
        print(repr(original_path)) # Use print to see that path is correct,repr show all
        fp = open(original_path)
Output:
'C:/code/img/test.png' 'C:/code/img/WeatherIconsclear-day.png'
No error so the files get open bye open().
The part that easy can go wrong is original_path = directory + file like missing a / or \\(never single \ in path) in the concatenate.
Reply
#6
Sorry I'm new to programming so what do I need to add to the code to fix it.
I have tried listdir(directory)

Output
Clear-day.png
Clesr-night.png
Cloudy.png
Fog.png
Partly-cloudy-day.png
Partly-cloudy-night.png
Rain.png
Sleet.png
Snow.png
Wind.pnd

So it is showing all the files and working


I have tried the print(repr(original_path))

Output
'C:Users\\user\\Documents\\Raspberrypi-Noticeboard-master\\WeatherIconsclear-days.png'

So path seems correct but still the same errno 2
Reply
#7
Do exactly what i have showed,just with your folder.
So first part for you based on error message should be.
import os

directory = 'C:\\Users\\user\\Documents\\RaspberryPi-Noticeboard-master\\'
for file in os.listdir(directory):
    print(file)
Also you shall run this code and see if WeatherIconsclear-day.png is in the output.
If you see that file add second part,just the same as i have posted.

The path C:\\Users\\user\\Documents is a little strange.
For Window the default path to Documents is C:\Users\<user name>\Documents.
So eg if i should navigate in cmd to Documents it look like this.
C:\>cd Users
C:\Users>cd tom # My user name
C:\Users\Tom>cd Documents
C:\Users\Tom\Documents>
Reply
#8
I have tried listdir(directory)

Output
Clear-day.png
Clesr-night.png
Cloudy.png
Fog.png
Partly-cloudy-day.png
Partly-cloudy-night.png
Rain.png
Sleet.png
Snow.png
Wind.pnd

So it is showing all the files and working


I have tried the print(repr(original_path))

Output
'C:Users\\user\\Documents\\Raspberrypi-Noticeboard-master\\WeatherIconsclear-day.png'

So path seems correct but still the same errno 2
Reply
#9
(Mar-16-2020, 12:06 AM)saqib1066 Wrote: I have tried listdir(directory)
I do not see WeatherIconsclear-days.png there.

(Mar-16-2020, 12:06 AM)saqib1066 Wrote: I have tried the print(repr(original_path))

Output
'C:Users\\user\\Documents\\Raspberrypi-Noticeboard-master\\WeatherIconsclear-days.png'
Look closer two missing \\,shall bee C:\\Users\\user...
Reply
#10
The WeatherIcons is a folder and inside there is

Clear-day.png
Clesr-night.png
Cloudy.png
Fog.png
Partly-cloudy-day.png
Partly-cloudy-night.png
Rain.png
Sleet.png
Snow.png
Wind.png



import os

directory = r'C:/Users/user/Documents/RaspberryPi-Noticeboard-master/WeatherIcons'
for file in os.listdir(directory):
print(file)



output
clear-day.png
clear-night.png
cloudy.png
fog.png
forecast
partly-cloudy-day.png
partly-cloudy-night.png
rain.png
sleet.png
snow.png
todays
wind.png
>>>


The missing \\ that's my mistake on this message I accidentally missed typing them

output
'C:\\Users\\user\\Documents\\RaspberryPi-Noticeboard-master\\WeatherIconsclear-day.png'
Traceback (most recent call last):
File "C:\Users\user\Documents\hi.py", line 306, in <module>
window.resize_icons()
File "C:\Users\user\Documents\hi.py", line 285, in resize_icons
fp = open(original_path)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Documents\\RaspberryPi-Noticeboard-master\\WeatherIconsclear-day.png'
>>>
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 281 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,592 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,393 Jun-27-2023, 01:17 PM
Last Post: diver999
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 1,011 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,133 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,564 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 1,037 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 1,665 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,672 Sep-12-2022, 04:50 AM
Last Post: python202209
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,388 May-17-2022, 07:49 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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