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
#21
Why do you not do as i posted,it's not so hard to delete a line an upgrade pillow.
I am not inserting in running all code,that mean i have to register to News API and Dark Sky to get two API keys.
Reply
#22
I've tried updating pillow it hasn't work and I can let you use my api keys if you want.
Reply
#23
I have tested code and done fix where needed,the code was also written in Python 2.
So i have also changed so it now work for Python 3.

Here how it look for me,i run Python 3.7 and Win-10.
[Image: 6eElSW.png]
from newsapi import NewsApiClient
from tkinter import *
from decimal import *
import webbrowser
import PIL.Image
import datetime
import requests
import time
import json
import sys
import dis
import os

directory = 'C:\\code\\img\\WeatherIcons\\'  # Where you stored your collection of weather icons
time1 = ''  # used in clock
news_api_key = 'your key'  # News API key
weather_api_key = 'your key'  # Dark Sky API key
width = 100  # Width of four day forecast icons in pixels
height = 100  # Height of four day forecast icons in pixels
degree_sign = u'\N{DEGREE SIGN}'  # unicode degrees sign for temperature


# *****Headline Class***** #
class Headline:

    # Headline Object consists of a button and an onClick() method
    def __init__(self, headline, url, parent_frame):
        self.Btn = Button(parent_frame, justify=LEFT, wraplength=250, text=headline, command=self.onClick,
                          highlightbackground='black', highlightcolor='black', highlightthickness=1)
        self.url = url

    def onClick(self):
        webbrowser.open(self.url)  # When pressed open url in web browser


# *****Following 4 day forecast Tkinter layout***** #
class DailyWeather:
    days_of_the_week = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']  # list of abbreviated weekdays
    today = datetime.datetime.today().weekday()  # get integer value for current day of the week

    # index is it's index of the list produced by getWeather, this identifies it's numerical distance from today's day of the week
    # forecast_icons is a dictionary consisting of the icon key names parameter and the local path to the corresponding icon image file
    def __init__(self, icon, temp, index, parent_frame, forecast_icons):
        self.frame = Frame(parent_frame, highlightbackground='black', highlightcolor='black',
                           highlightthickness=1)  # create frame inside parent_frame
        # index determines day of the week and therefore placement relative to today's weather
        if index == 0:
            self.frame.grid(column=2, row=1, sticky=N + S + E + W)
        if index == 1:
            self.frame.grid(column=0, row=2, sticky=N + S + E + W)
        if index == 2:
            self.frame.grid(column=1, row=2, sticky=N + S + E + W)
        if index == 3:
            self.frame.grid(column=2, row=2, sticky=N + S + E + W)

        # configure the 3 by 3 layout to scale when there is available space
        for i in range(0, 3):
            self.frame.rowconfigure(i, weight=1)
            self.frame.columnconfigure(i, weight=1)

        self.fp = forecast_icons.get(icon)  # get file path of the icon
        self.img = PhotoImage(
            file=self.fp)  # convert image into Tkinter PhotoImage object so it can be placed in a label
        self.icon = Label(self.frame, image=self.img)  # create icon label
        self.temp = Label(self.frame, text=str(temp) + ' ' + degree_sign + 'C')  # create temperature label
        self.day_name = self.days_of_the_week[
            (self.today + index + 1) % len(
                self.days_of_the_week)]  # use modulo to make list circular and retrieve relative day of the week
        self.day = Label(self.frame, text=self.day_name)  # create day of the week label

    # place the objects labels in the proper spots
    def place(self):
        self.icon.grid(row=0, column=0, rowspan=3, columnspan=2, sticky=N + S + E + W)
        self.day.grid(row=0, column=2, sticky=N + S + E + W)
        self.temp.grid(row=2, column=2, sticky=N + S + E + W)


# *****Today's Weather Label Layout***** #
class TodaysWeather:

    # icon_fp is the filepath to today's weather icon
    def __init__(self, icon_fp, temp, summary, parent_frame):
        self.todaysWeatherFrame = Frame(parent_frame, highlightbackground='black', highlightcolor='black',
                                        highlightthickness=1)  # create frame for today's weather
        self.todaysWeatherFrame.grid(column=0, columnspan=2, row=0, rowspan=2, sticky=N + S + E + W)

        # configure todaysWeatherFrame to scale
        for i in range(0, 3):
            self.todaysWeatherFrame.rowconfigure(i, weight=1)
            self.todaysWeatherFrame.columnconfigure(i, weight=1)

        # image process is the same as explained in DailyWeather
        self.fp = icon_fp
        self.img = PhotoImage(file=self.fp)
        self.icon = Label(self.todaysWeatherFrame, image=self.img)
        self.temp = Label(self.todaysWeatherFrame, text=str(temp) + ' ' + degree_sign + 'C',
                          justify=LEFT)
        self.summary = Label(self.todaysWeatherFrame, text=summary)
        self.today = Label(self.todaysWeatherFrame, text='Today', justify=LEFT)

    def place(self):
        self.icon.grid(row=0, column=0, rowspan=2, columnspan=3, sticky=N + S + E + W)
        self.temp.grid(row=1, column=3, sticky=N + S + E + W)
        self.summary.grid(row=2, column=0, columnspan=4, sticky=N + S + E + W)
        self.today.grid(row=0, column=3, sticky=N + S + E + W)


class MainWindow:

    def __init__(self, directory, news_api_key, weather_api_key, width, height):
        # initialize MainWindow object variables
        self.width = width  # width of DailyWeather icons
        self.height = height  # height of DailyWeather icons
        self.news_api_key = news_api_key
        self.weather_api_key = weather_api_key
        self.directory = directory #  directory of base weather icons
        self.buttonHeadlines = []  # list of headline buttons
        self.forecast = []  # list containing the next four days weather
        self.forecast_icons = {}  # dictionary of standardized weather keys, and their corresponding DailyWeather sized icon paths
        self.todays_icons = {}  # dictionary of standardized weather keys, and their corresponding TodaysWeather sized icon paths
        self.objects = {}  # holds daily weather objects
        self.state = False  # state for full screen toggle
        self.root = Tk()  # base Tk() object
        self.country = ''  # country code used for news headlines
        self.todays_weather_list = []

    def body(self):
        self.root.attributes('-fullscreen', True)  # start in fullscreen mode
        self.root.bind("<F11>", self.toggle_fullscreen)  # bind F11 as button to toggle fullscreen
        self.root.bind("<Escape>", self.end_fullscreen)  # bind Escape as a way to exit fullscreen
        self.root.columnconfigure(0, weight=1)  # making root scalable in y
        self.root.rowconfigure(0, weight=1)  # and x

        # ***Base Frame*** #
        frame = Frame(self.root)
        # allow frame to scale to the size of root
        frame.rowconfigure(0, weight=1)
        frame.columnconfigure(1, weight=1)
        frame.grid(sticky=N + S + E + W)  # sticky allows it to scale in all directions

        # ***Create Left Frame for News Headlines*** #
        leftFrame = Frame(frame, bg="red")
        leftFrame.grid(row=0, column=0, sticky=N + S + E + W)  # in the first row and column of frame

        # ***Making the headlines scalable*** #
        leftFrame.columnconfigure(0, weight=1)
        # top ten headlines, be displayed in 10 rows
        for i in range(0, 10):
            leftFrame.rowconfigure(i, weight=1)

        # ***Frame for clock and weather*** #
        rightFrame = Frame(frame)
        rightFrame.grid(column=1, row=0, sticky=N + S + E + W)
        # allow frame to scale
        rightFrame.columnconfigure(0, weight=1)
        rightFrame.rowconfigure(0, weight=1)

        # ***Frame for weather data*** #
        weatherFrame = Frame(rightFrame)
        weatherFrame.grid(column=0, row=0, sticky=N + S + E + W)
        # make rows and columns of weather data scalable
        for i in range(0, 3):
            weatherFrame.rowconfigure(i, weight=1)
            weatherFrame.columnconfigure(i, weight=1)

        # *****Weather***** #
        todays_forecast, four_day_forecast = self.getWeather()

        # *** Four Day Forecast*** #
        for i, day in enumerate(four_day_forecast):
            item = DailyWeather(day[0], day[1], i, weatherFrame, self.forecast_icons)  # day[0] = icon, day[1] = avgTemp
            self.objects['item' + str(i)] = item  # store in dictionary to avoid garbage collection on image
        for item in self.objects:
            self.objects[item].place()  # place the DailyWeather Items

        # ***Today's weather*** #
        icon_fp = self.todays_icons.get(todays_forecast[0])
        temp = todays_forecast[1]
        summary = todays_forecast[2]
        todays_weather = TodaysWeather(icon_fp, temp, summary, weatherFrame)
        self.todays_weather_list.append(todays_weather)  # store it in list so that garbage collection doesn't throw out the image
        self.todays_weather_list[0].place()  # place stored TodaysWeather object

        # ***Headlines*** #
        for title in self.getNews(self.country):  # takes in country code for relevant news
            headline = Headline(title[0], title[1], leftFrame)  # create Headline object with each headline. title[0] = title or article, title[1] = url of article
            self.buttonHeadlines.append(headline)  # create list of headline objects

        # ***Clock Placeholder*** #
        Clock = Label(weatherFrame, font=('helvetica', 20, 'bold'), highlightbackground='black',
                      highlightcolor='black', highlightthickness=1)

        # ***Packing in Headlines*** 3
        for i, object in enumerate(self.buttonHeadlines):
            object.Btn.grid(row=i, sticky=N + S + E + W)

        # *****Clock Widget***** #
        def tick():
            global time1
            # get the current local time from the PC
            time2 = time.strftime('%H:%M:%S')
            # if time string has changed, update it
            if time2 != time1:
                time1 = time2
                Clock.config(text=time2)
            # calls itself every 200 milliseconds
            # to update the time display as needed
            # could use >200 ms, but display gets jerky
            Clock.after(200, tick)

        # ***Placing Clock*** #
        Clock.grid(column=2, row=0, sticky=N + S + E + W)
        tick()

    def toggle_fullscreen(self, event=None):
        self.state = not self.state  # Just toggling the boolean
        self.root.attributes("-fullscreen", self.state)

    def end_fullscreen(self, event=None):
        self.state = False
        self.root.attributes("-fullscreen", False)

    def getNews(self, country):
        newsapi = NewsApiClient(api_key=self.news_api_key)
        newsHeadlines = []

        top_headlines = newsapi.get_top_headlines(country=country)
        response = top_headlines  # this is already in json format, hype
        top_ten_headlines = response['articles'][:10]  # take top 10 headlines
        for articles in top_ten_headlines:
            newsHeadlines.append((articles['title'], articles['url'].encode('utf-8')))
        return newsHeadlines  # return list of tuples

    def getWeather(self):
        four_day_forecast = []  # list of tuples[(icon, avgTemp)]

        # ***Use IP to get Lat/Long for local weather
        r = requests.get('http://ip-api.com/json')
        response = r.text
        response = json.loads(response)
        lat_long = str(response['lat']) + ', ' + str(response['lon'])
        self.country = str(response['countryCode'])
        self.country = self.country.lower()

        # ***API request to get weather data
        url = 'https://api.darksky.net/forecast/' + self.weather_api_key + '/' + lat_long
        weather = requests.get(url, params={'exclude': 'minutely,alerts,flags', 'units': 'ca'})
        weather = weather.json()

        # ***Today's weather info*** #
        todays_weather_raw = weather['hourly']['data'][0]
        todays_summary = todays_weather_raw['summary']  # summary of today's weather
        todays_icon = todays_weather_raw['icon']  # icon for today's weather
        temp = Decimal(todays_weather_raw['temperature'])
        temp = round(temp, 1)
        todays_temp = temp  # current temp
        todays_weather = [todays_icon, todays_temp, todays_summary]

        # ***Next 4 days weather*** #
        for i in range(1, 5):
            daily_weather = weather['daily']['data'][i]
            x = Decimal((daily_weather['temperatureHigh'] + daily_weather['temperatureLow']) / 2)
            avg_temp = round(x, 1)
            four_day_forecast.append((daily_weather['icon'], avg_temp))
            # now just have to reference icon title to a library of icons for representation in gui
        return todays_weather, four_day_forecast

        # *****Resize Icons***** #

    def resize_icons(self):
        # ***Create Subdirectories for resized icons*** #
        subdir_forecast = self.directory + 'forecast'  # directory for daily forecast sized icons
        subdir_todays = self.directory + 'todays'  # directory for today's weather sized icons
        # if they don't already exist
        if not os.path.exists(subdir_forecast):
            os.makedirs(subdir_forecast)
        if not os.path.exists(subdir_todays):
            os.makedirs(subdir_todays)
        # ***Iterate through icons directory resizing each image*** #
        for file in os.listdir(directory):
            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
                #fp = open(original_path)
                img = PIL.Image.open(original_path)
                resizedImage = img.resize((self.width, self.height), PIL.Image.ANTIALIAS)
                resizedImage.save(subdir_forecast + '/' + file, "PNG")  # save forecast sized icon
                #fp.close()
                resizedImage.close()
                # repeat above for double the size today's icons
                #fp = open(original_path)
                img = PIL.Image.open(original_path)
                resizedImage = img.resize((int(2 * width), int(2 * height)), PIL.Image.ANTIALIAS) # adjust the multiplying factor of height and width
                resizedImage.save(subdir_todays + '/' + file, "PNG")
                #fp.close()
                resizedImage.close()
                # fill dictionary for reference in filling label
                name, extension = file.split(".")
                self.forecast_icons[name] = str(subdir_forecast + '/' + file)
                self.todays_icons[name] = str(subdir_todays + '/' + file)


if __name__ == '__main__':
    window = MainWindow(directory, news_api_key, weather_api_key, width, height)
    window.resize_icons()
    window.body()
    window.root.mainloop()
Reply
#24
Thank you so much you are a genius.
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 134 Yesterday, 01:46 PM
Last Post: Username_Python1
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,496 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,086 Jun-27-2023, 01:17 PM
Last Post: diver999
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 928 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,071 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,495 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 985 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 1,616 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,530 Sep-12-2022, 04:50 AM
Last Post: python202209
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,331 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