Last active
August 29, 2015 14:19
-
-
Save psychemedia/fbcd7cf1daabe0004e27 to your computer and use it in GitHub Desktop.
Open Knowledge / Cabinet Office Code Club, week 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##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