Skip to content

Instantly share code, notes, and snippets.

@ghl3
Created January 23, 2013 18:30
Show Gist options
  • Select an option

  • Save ghl3/4611323 to your computer and use it in GitHub Desktop.

Select an option

Save ghl3/4611323 to your computer and use it in GitHub Desktop.
Open a local file or, if it doesn't exist, grab data from a url, cache it as a local file, and then open that file
data_url = "https://raw.github.com/pydata/pydata-book/master/ch02/usagov_bitly_data2012-03-16-1331923249.txt"
data_path = "usagov_bitly_data2012-03-16-1331923249.txt"
try:
data = open(data_path)
except IOError:
request = urllib2.Request(data_url)
data = urllib2.urlopen(request).read()
with open(data_path, "w+") as data_file:
data_file.write(data)
data = open(data_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment