Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python example to download files from an anonymous FTP server (example case from 1000 Genomes)
#!/usr/bin/env python
from __future__ import print_function
import ftplib, datetime, sys
# ftp://ftp.1000genomes.ebi.ac.uk/vol1/ftp/release/20130502/ALL.chr14.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz
def log(msg):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %T")
print('[-- {} --] {}'.format(timestamp, msg), file=sys.stderr)
server = 'ftp.1000genomes.ebi.ac.uk'
directory = 'vol1/ftp/release/20130502'
vcf = 'ALL.chr14.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz.tbi'
log("connecting to server")
ftp = ftplib.FTP(server)
ftp.login()
log("changing to directory: {}".format(directory))
ftp.cwd(directory)
ftp.retrlines('LIST')
log("starting to download: {}".format(vcf))
ftp.retrbinary("RETR {}".format(vcf), open(vcf, 'wb').write)
log("finished download")
ftp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment