来自url的下载文件出错

时间:2015-07-15 03:35:00

标签: python file url download

的问候。 当我从网址(ftp://garner.ucsd.edu/pub/rinex/)下载文件时出现问题,我自动下载,但是当他们更改URL和名称文件的值时我会遇到问题。当我运行脚本时,进行了第一次下载,然后是错误......故障在哪里?

#!/usr/bin/python

import shutil
import urllib
import urllib2
import sys, os
import time, datetime
import numpy as np

def cods(n):
    codes = ['antc', 'areq', 'arev', 'bogt', 'braz', 'brft', 'bue1', 'bue2', 'cfag', 'chpi', \
    'copo', 'cord', 'coyq', 'falk', 'glps', 'iqqe', 'koug', 'kour', 'ispa', 'lpgs', \
    'mgue', 'parc', 'pove', 'qui1', 'qui2', 'recf', 'rio2', 'riop', 'salu', 'sant', \
    'savo', 'sptu', 'ufpr', 'unsa', 'valp']

    return codes[n]

def file_exists(location):
    request = urllib2.Request(location)
    request.get_method = lambda : 'HEAD'

    try:
        response = urllib2.urlopen(request)
        return True

    except IOError:
        return False

if __name__=='__main__':

    days = (datetime.datetime(yy,mm,dd) -datetime.datetime(yy,1,1,0,0,0)).days

    for yy in np.arange(2010, 2015, 1):
        days = (datetime.datetime(yy+1,1,1,0,0,0)-datetime.datetime(yy,1,1,0,0,0)).days
        print 'Total days in '+str(yy)+': '+str(days)

        for dd in np.arange(1, days+1, 1):
            urldir   = 'ftp://garner.ucsd.edu/pub/rinex/'+str('%04d'%yy)+'/'+str('%03d'%dd)+'/'

            for k in np.arange(0, 35, 1): # k is len(codes)
                gpsfile  = cods(k)+str('%03d'%dd)+'0.'+str('%04d'%yy)[2:4]+'d.Z'

                if file_exists(urldir+gpsfile)==True:
                    print 'Downloading '+gpsfile 
                    urllib.urlretrieve(urldir, gpsfile)

        else:
            print 'File no found '+gpsfile[j]  

错误显示

Downloading   antc0010.08d.Z
Downloading   areq0010.08d.Z
File no found arev0010.08d.Z
Downloading   bogt0010.08d.Z
Downloading   braz0010.08d.Z
Downloading   brft0010.08d.Z
.
.
.
.
File no found sptu0010.08d.Z
File no found ufpr0010.08d.Z
Downloading   unsa0010.08d.Z
File no found valp0010.08d.Z
Downloading   antc0020.08d.Z
Traceback (most recent call last):
  File "GPSDataSouth.py", line 69, in <module>
    urllib.urlretrieve(urldir+gpsfile, gpsfile)
  File "/usr/lib/python2.7/urllib.py", line 94, in urlretrieve
    return _urlopener.retrieve(url, filename, reporthook, data)
  File "/usr/lib/python2.7/urllib.py", line 240, in retrieve
    fp = self.open(url, data)
  File "/usr/lib/python2.7/urllib.py", line 208, in open
    return getattr(self, name)(url)
  File "/usr/lib/python2.7/urllib.py", line 544, in open_ftp
    ftpwrapper(user, passwd, host, port, dirs)
  File "/usr/lib/python2.7/urllib.py", line 868, in __init__
    self.init()
  File "/usr/lib/python2.7/urllib.py", line 874, in init
    self.ftp.connect(self.host, self.port, self.timeout)
  File "/usr/lib/python2.7/ftplib.py", line 138, in connect
    self.welcome = self.getresp()
  File "/usr/lib/python2.7/ftplib.py", line 215, in getresp
    resp = self.getmultiline()
  File "/usr/lib/python2.7/ftplib.py", line 201, in getmultiline
    line = self.getline()
  File "/usr/lib/python2.7/ftplib.py", line 191, in getline
    if not line: raise EOFError
IOError: [Errno ftp error] 
usuario@geodesia1405:~/Escritorio/GPS_South/GetDataScript$ 

0 个答案:

没有答案