Reputation: 1
I am currently learning python and reached the end of a section in my teachings so I thought I would try building a few basic projects with what I have learned so far.
I found this file on git and thought I would recreate it and modify it a bit to allow user input so that they can tailor their own city/location
However, when I run the script I get an error. Please see the code below and the error under that. I didn't know if should put the entire error here or just the last lines so I thought I'd err on the safe side and put all of it. Apologies if it's really long and obnoxious.
import urllib
import json
previous_weather_file = "weather_log.txt"
previous_weather = ""
try:
log = open(previous_weather_file, "r")
previous_weather = log.read()
log.close()
except:
print "No previous data"
city_name = raw_input("What is the city name you would like to check the weather for? ")
f = urllib.urlopen("api.openweathermap.org/data/2.5/weather?q={city_name}")
weather = f.read()
log = open(previous_weather_file, "w")
log.write(weather)
log.close()
weather_json = json.load(weather)
#print weather
#print weather_json['weather']
curr_temp = float(weather_json['main']['temp']) - 273.13
print "Temperature is %.2f degrees C" % (curr_temp)
if (not previous_weather == ""):
previous_weather_json = json.load(previous_weather)
prev_temp = float(previous_weather_json['main']['temp']) - 273.13
temp_diff = curr_temp - prev_temp
if not (temp_diff == 0.0):
print "Temperature has changed by: %.2f degrees C" % (temp_diff)
#error message
Serxhios-MBP:Projects SerxhioZefi$ python weather_get.py
What is the city name you would like to check the weather for? London
Traceback (most recent call last):
File "weather_get.py", line 16, in <module>
f = urllib.urlopen("api.openweathermap.org/data/2.5/weather?q={city_name}")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 87, in urlopen
return opener.open(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 469, in open_file
return self.open_local_file(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 483, in open_local_file
raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: 'api.openweathermap.org/data/2.5/weather?q={city_name}'
Upvotes: 0
Views: 899
Reputation: 1579
Change line 16 to this (you are missing the http or https for the API)
f = urllib.urlopen("http://api.openweathermap.org/data/2.5/weather?q={city_name}")
Next you will run into ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x104827a70>)
but that is another question, having to do with the openweathermap API. I would check their docs for the API usage. Probably you will need to include an authentication key with your requests.
I recommend the requests
module for this type of work in python, mainly because it is enjoyable to use and simplifies many tasks:
http://docs.python-requests.org/en/master/
Upvotes: 2