Skip to content

Instantly share code, notes, and snippets.

@indraniel
Created May 26, 2017 23:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save indraniel/02b16a2c3d0ff6518e0e025483791c59 to your computer and use it in GitHub Desktop.
Save indraniel/02b16a2c3d0ff6518e0e025483791c59 to your computer and use it in GitHub Desktop.
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