Error trying to download zip from BMF with python

1

I'm trying to download BMF zip files at a time ( ftp://ftp.bmf.com.br/ But there is an error in the program that does not find the file, but it does exist.

follow the code:

from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile
import pandas as pd

startdate = '20150130'
enddate = '20150210'
extension = '.zip'
daterange = pd.date_range(startdate, enddate)

for single_date in daterange:
zipurl = 'ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_' + single_date.strftime('%Y%m%d') + extension
with urlopen(zipurl) as zipresp:
    with ZipFile(BytesIO(zipresp.read())) as zfile:
        zfile.extractall('C:/Users/.../.../...')

The error that appears is basically that it does not find the file ...

  

ftplib.error_perm: 550 NEG_BMF_20150131.zip: The system can not find   the file specified ..

Is there an easier way to do this?

    
asked by anonymous 12.04.2017 / 00:34

1 answer

0

Your code is almost perfect. Very good!

I decided to test your code with the following dates:

startdate = '20170130'
enddate = '20170207'

When testing with a shorter period, I could see the following:

  • This error only appears when there were no trades on the day (Sat, Sun or Holidays), ie the file does not really exist!
  • You can use a try / except structure to work around this error

Example Code:

from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile
import pandas as pd

startdate = '20170130'
enddate = '20170207'
extension = '.zip'
daterange = pd.date_range(start=startdate, end=enddate)

for single_date in daterange:
    zipurl = 'ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_' + single_date.strftime('%Y%m%d') + extension
    try:
        with urlopen(zipurl) as zipresp:
            with ZipFile(BytesIO(zipresp.read())) as zfile:
                zfile.extractall()
        print("OK: {}".format(zipurl))
    except:
        print('ERRO: {}'.format(zipurl))

    print("="*5)

Output:

OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170130.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170131.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170201.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170202.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170203.zip
=====
ERRO: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170204.zip
=====
ERRO: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170205.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170206.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170207.zip
=====

Checking in the calendar, we can see that 04/02/2017 was a sábado and 05/02/2017 a domingo .

And when entering the folder with the supposed downloads, surprise:

Note that I removed the extraction output path for testing purposes. You can reinstall it in your code (% with%)

    
12.04.2017 / 13:28