Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Last active August 29, 2015 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psychemedia/fbcd7cf1daabe0004e27 to your computer and use it in GitHub Desktop.
Save psychemedia/fbcd7cf1daabe0004e27 to your computer and use it in GitHub Desktop.
Open Knowledge / Cabinet Office Code Club, week 8
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#Test if your file exists at the path you're trying...
def isItThere(myfile):
with open(myfile) as myfile:
head = [next(myfile) for x in xrange(2)]
return head
#eg
myfile='whatever.csv'
isItThere(myfile)
#or
myfile='wherever/whatever.csv'
isItThere(myfile)
##Via http://stackoverflow.com/a/16696317/454773
#TH - added path and directory creator
#The following function seems to be a pretty robust, generic function,
#for downloading an arbitrary file to our local machine
import requests
import os
def download_file(url,path=''):
''' Download a file from a particular URL to current directory/path with original filename '''
local_filename = url.split('/')[-1]
local_filename='/'.join([path,local_filename])
#Create a new directory if it doesn't already exist
if not os.path.exists(path):
os.makedirs(path)
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
#url='https://raw.githubusercontent.com/datasets/geo-boundaries-world-110m/master/countries.geojson'
#download_file(url,path='newdemo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment