Read a file in buffer from FTP python

后端 未结 3 1333
谎友^
谎友^ 2020-12-01 13:06

I am trying to read a file from an FTP server. The file is a .gz file. I would like to know if I can perform actions on this file while the socket is open. I tr

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 13:42

    There are two easy ways I can think of to download a file using FTP and store it locally:

    1. Using ftplib:

      from ftplib import FTP
      
      ftp = FTP('ftp.ncbi.nlm.nih.gov')
      ftp.login()
      ftp.cwd('pub/pmc')
      ftp.retrbinary('RETR PMC-ids.csv.gz', open('PMC-ids.csv.gz', 'wb').write)
      ftp.quit()
      
    2. Using urllib

      from urllib import urlretrieve
      
      urlretrieve("ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/PMC-ids.csv.gz", "PMC-ids.csv.gz")
      

    If you don't want to download and store it to a file, but you want to process it gradually as it comes, I suggest using urllib2:

    from urllib2 import urlopen
    
    u = urlopen("ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/readme.txt")
    
    for line in u:
       print line
    

    which prints your file line by line.

提交回复
热议问题