Skip to content

Instantly share code, notes, and snippets.

@flibbertigibbet
Created December 23, 2013 06:20
Show Gist options
  • Save flibbertigibbet/8092460 to your computer and use it in GitHub Desktop.
Save flibbertigibbet/8092460 to your computer and use it in GitHub Desktop.
Download TIGER/Line data from the Census FTP server based on state FIPS code. Here, pulls EDGE data for four states.
#!/usr/bin/env python
from os import walk
from ftplib import FTP
from time import sleep
# 10 - DE
# 34 - NJ
# 36 - NY
# 42 - PA
fips = ['10', '34', '36', '42']
ftp = FTP('ftp2.census.gov')
ftp.login()
ftp.cwd('/geo/tiger/TIGER2013/EDGES/')
my_list = []
def get_mine(ln):
lns = ln.split(' ')
f = lns[-1:][0]
if f[8:10] in fips:
print(f)
my_list.append(f)
ftp.retrlines('LIST', get_mine)
for f in my_list:
print('getting ' + f)
ftp.retrbinary('RETR ' + f, open(f, 'wb').write)
sleep(5)
ftp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment