-2

I am new at python. I want to download through a code data from this URL: "ftp://cddis.nasa.gov/gnss/products/ionex/". However the files that I want have this format: "codgxxxx.xxx.Z". All these files are inside each year(enter image description here) as it is show here:enter image description here. How can I download it just those files using python?.

Until now I have been using wget with this code: wget ftp://cddis.nasa.gov/gnss/products/ionex/2008/246/codg0246.07i.Z", for each one of files but is to tedious.

Can anyone help me please!!.

Thank you

Sie. Theos
  • 1
  • 1
  • 1
  • 2
    Welcome to Stack Exchange. Your question is a bit unclear can you give more information about what you are trying to do? Are you trying to save file from FTP or access them? Stack Exchange isn't a code writing service so be sure to include the code you have tried so far. Check out [How to Ask](https://stackoverflow.com/help/how-to-ask). – Polkaguy6000 Feb 06 '19 at 17:39

1 Answers1

0

Since you know the structure in the FTP server, this can be pretty easy to accomplish without having to use ftplib.

It would be cleaner to actually retrieve a directory listing from the server such as this question

(I don't seem to be able to connect to that nasa URL though)

I would recommend reading here for more on how to actually perform an FTP download.

But something like this may work. (full disclosure: I haven't tested it)

import urllib
YEARS_TO_DOWNLOAD = 12
BASE_URL = "ftp://cddis.nasa.gov/gnss/products/ionex/"
FILE_PATTERN = "codg{}.{}.Z"
SAVE_DIR = "/home/your_name/nasa_ftp/"
year = 2006
three_digit_number = 0

for i in range(0, YEARS_TO_DOWNLOAD):
    target = FILE_PATTERN.format(str(year + i), str(three_digit_number.zfill(3))
    try:
        urllib.urlretrieve(BASE_URL + target, SAVE_DIR + target)
    except urllib.error as e:
        print("An error occurred trying to download {}.\nReason: {}".format(target, 
              str(e))
    else:
        print("{} -> {}".format(target, SAVE_DIR + target))

print("Download finished!")
John S.
  • 26
  • 1
  • 4