Python从公共FTP服务器下载zip文件
我需要从我的PC(windows OS)上的公共地理数据库的“C:\DEMDownload”文件夹中下载几个(数字地球模型)zip文件 当我在Python从公共FTP服务器下载zip文件,python,ftp,runtime-error,download,Python,Ftp,Runtime Error,Download,我需要从我的PC(windows OS)上的公共地理数据库的“C:\DEMDownload”文件夹中下载几个(数字地球模型)zip文件 当我在ftp.retrbinary('RETR%s'%file,open(local_file,'wb').write)行运行代码时,我收到以下错误消息 Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Python27\
ftp.retrbinary('RETR%s'%file,open(local_file,'wb').write)行运行代码时,
我收到以下错误消息
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 414, in retrbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
error_perm: 550 Failed to open file.
我可以通过以下方式成功下载具有给定url的zip文件:
# connect to ftp
url = urlparse.urlparse("http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/")
ftp = ftplib.FTP(url.netloc)
ftp.login()
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
通过遍历ftp目录,可以避免硬编码的目录/文件名,这与创造性地使用
完整代码如下:
url = 'http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/'
url = urlparse.urlparse(url)
local_root = os.path.expanduser("~/ftp_download") # change this to wherever you want to download to
def download(ftp, ftp_path, filename, check_cwd=True):
"""
Using the given ftp connection, download from ftp_path to
filename.
If check_cwd is False, assume the ftp connection is already
in the correct current working directory (cwd)
"""
basename = posixpath.basename(ftp_path)
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
if check_cwd:
ftp_dirname = posixpath.dirname(ftp_path)
if ftp_dirname != ftp.pwd():
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
def ftp_dir(ftp):
"""
Given a valid ftp connection, get a list of 2-tuples of the
files in the ftp current working directory, where the first
element is whether the file is a directory and the second
element is the filename.
"""
# use a callback to grab the ftp.dir() output in a list
dir_listing = []
ftp.dir(lambda x: dir_listing.append(x))
return [(line[0].upper() == 'D', line.rsplit()[-1]) for line in dir_listing]
# connect to ftp
ftp = ftplib.FTP(url.netloc)
ftp.login()
# recursively walk through the directory and download each file, depth first
stack = [url.path]
while stack:
path = stack.pop()
ftp.cwd(path)
# add all directories to the queue
children = ftp_dir(ftp)
dirs = [posixpath.join(path, child[1]) for child in children if child[0]]
files = [posixpath.join(path, child[1]) for child in children if not child[0]]
stack.extend(dirs[::-1]) # add dirs reversed so they are popped out in order
# download all files in the directory
for filepath in files:
download(ftp, filepath, os.path.join(local_root, filepath.split(url.path,1)[-1]),
check_cwd=False)
# logout
ftp.quit()
您可以通过使用一个python ftp包装库(如或)来进一步压缩此内容。我可以通过以下方式成功下载具有给定url的zip文件:
# connect to ftp
url = urlparse.urlparse("http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/")
ftp = ftplib.FTP(url.netloc)
ftp.login()
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
通过遍历ftp目录,可以避免硬编码的目录/文件名,这与创造性地使用
完整代码如下:
url = 'http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/'
url = urlparse.urlparse(url)
local_root = os.path.expanduser("~/ftp_download") # change this to wherever you want to download to
def download(ftp, ftp_path, filename, check_cwd=True):
"""
Using the given ftp connection, download from ftp_path to
filename.
If check_cwd is False, assume the ftp connection is already
in the correct current working directory (cwd)
"""
basename = posixpath.basename(ftp_path)
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
if check_cwd:
ftp_dirname = posixpath.dirname(ftp_path)
if ftp_dirname != ftp.pwd():
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
def ftp_dir(ftp):
"""
Given a valid ftp connection, get a list of 2-tuples of the
files in the ftp current working directory, where the first
element is whether the file is a directory and the second
element is the filename.
"""
# use a callback to grab the ftp.dir() output in a list
dir_listing = []
ftp.dir(lambda x: dir_listing.append(x))
return [(line[0].upper() == 'D', line.rsplit()[-1]) for line in dir_listing]
# connect to ftp
ftp = ftplib.FTP(url.netloc)
ftp.login()
# recursively walk through the directory and download each file, depth first
stack = [url.path]
while stack:
path = stack.pop()
ftp.cwd(path)
# add all directories to the queue
children = ftp_dir(ftp)
dirs = [posixpath.join(path, child[1]) for child in children if child[0]]
files = [posixpath.join(path, child[1]) for child in children if not child[0]]
stack.extend(dirs[::-1]) # add dirs reversed so they are popped out in order
# download all files in the directory
for filepath in files:
download(ftp, filepath, os.path.join(local_root, filepath.split(url.path,1)[-1]),
check_cwd=False)
# logout
ftp.quit()
您可以通过使用一个python ftp包装库(如或)来进一步压缩此内容。我可以通过以下方式成功下载具有给定url的zip文件:
# connect to ftp
url = urlparse.urlparse("http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/")
ftp = ftplib.FTP(url.netloc)
ftp.login()
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
通过遍历ftp目录,可以避免硬编码的目录/文件名,这与创造性地使用
完整代码如下:
url = 'http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/'
url = urlparse.urlparse(url)
local_root = os.path.expanduser("~/ftp_download") # change this to wherever you want to download to
def download(ftp, ftp_path, filename, check_cwd=True):
"""
Using the given ftp connection, download from ftp_path to
filename.
If check_cwd is False, assume the ftp connection is already
in the correct current working directory (cwd)
"""
basename = posixpath.basename(ftp_path)
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
if check_cwd:
ftp_dirname = posixpath.dirname(ftp_path)
if ftp_dirname != ftp.pwd():
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
def ftp_dir(ftp):
"""
Given a valid ftp connection, get a list of 2-tuples of the
files in the ftp current working directory, where the first
element is whether the file is a directory and the second
element is the filename.
"""
# use a callback to grab the ftp.dir() output in a list
dir_listing = []
ftp.dir(lambda x: dir_listing.append(x))
return [(line[0].upper() == 'D', line.rsplit()[-1]) for line in dir_listing]
# connect to ftp
ftp = ftplib.FTP(url.netloc)
ftp.login()
# recursively walk through the directory and download each file, depth first
stack = [url.path]
while stack:
path = stack.pop()
ftp.cwd(path)
# add all directories to the queue
children = ftp_dir(ftp)
dirs = [posixpath.join(path, child[1]) for child in children if child[0]]
files = [posixpath.join(path, child[1]) for child in children if not child[0]]
stack.extend(dirs[::-1]) # add dirs reversed so they are popped out in order
# download all files in the directory
for filepath in files:
download(ftp, filepath, os.path.join(local_root, filepath.split(url.path,1)[-1]),
check_cwd=False)
# logout
ftp.quit()
您可以通过使用一个python ftp包装库(如或)来进一步压缩此内容。我可以通过以下方式成功下载具有给定url的zip文件:
# connect to ftp
url = urlparse.urlparse("http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/")
ftp = ftplib.FTP(url.netloc)
ftp.login()
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
通过遍历ftp目录,可以避免硬编码的目录/文件名,这与创造性地使用
完整代码如下:
url = 'http://ftp2.cits.rncan.gc.ca/pub/geobase/official/cded/50k_dem/'
url = urlparse.urlparse(url)
local_root = os.path.expanduser("~/ftp_download") # change this to wherever you want to download to
def download(ftp, ftp_path, filename, check_cwd=True):
"""
Using the given ftp connection, download from ftp_path to
filename.
If check_cwd is False, assume the ftp connection is already
in the correct current working directory (cwd)
"""
basename = posixpath.basename(ftp_path)
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
if check_cwd:
ftp_dirname = posixpath.dirname(ftp_path)
if ftp_dirname != ftp.pwd():
ftp.cwd(ftp_dirname)
with open(filename, 'w') as fobj:
ftp.retrbinary('RETR %s' % basename, fobj.write)
def ftp_dir(ftp):
"""
Given a valid ftp connection, get a list of 2-tuples of the
files in the ftp current working directory, where the first
element is whether the file is a directory and the second
element is the filename.
"""
# use a callback to grab the ftp.dir() output in a list
dir_listing = []
ftp.dir(lambda x: dir_listing.append(x))
return [(line[0].upper() == 'D', line.rsplit()[-1]) for line in dir_listing]
# connect to ftp
ftp = ftplib.FTP(url.netloc)
ftp.login()
# recursively walk through the directory and download each file, depth first
stack = [url.path]
while stack:
path = stack.pop()
ftp.cwd(path)
# add all directories to the queue
children = ftp_dir(ftp)
dirs = [posixpath.join(path, child[1]) for child in children if child[0]]
files = [posixpath.join(path, child[1]) for child in children if not child[0]]
stack.extend(dirs[::-1]) # add dirs reversed so they are popped out in order
# download all files in the directory
for filepath in files:
download(ftp, filepath, os.path.join(local_root, filepath.split(url.path,1)[-1]),
check_cwd=False)
# logout
ftp.quit()
您可以通过使用python ftp包装库(例如或)进一步压缩此内容。您想下载所有可用的zip文件或特定的zip文件吗?我在根目录中看到多个目录。我需要下载所有可用的zip文件每个目录001、002、003…中的所有zip文件?是的。我知道它们有很多文件(例如,所有加拿大的DEM模型。仅次于俄罗斯的第二大国家)好的,我也得到了同样的错误。你读过这篇文章吗:我会尽力让它对你的案子起作用。一旦它工作,你可以遍历每个目录。你想下载所有可用的zip文件或特定的zip文件吗?我在根目录中看到多个目录。我需要下载所有可用的zip文件每个目录001、002、003…中的所有zip文件?是的。我知道它们有很多文件(例如,所有加拿大的DEM模型。仅次于俄罗斯的第二大国家)好的,我也得到了同样的错误。你读过这篇文章吗:我会尽力让它对你的案子起作用。一旦它工作,你可以遍历每个目录。你想下载所有可用的zip文件或特定的zip文件吗?我在根目录中看到多个目录。我需要下载所有可用的zip文件每个目录001、002、003…中的所有zip文件?是的。我知道它们有很多文件(例如,所有加拿大的DEM模型。仅次于俄罗斯的第二大国家)好的,我也得到了同样的错误。你读过这篇文章吗:我会尽力让它对你的案子起作用。一旦它工作,你可以遍历每个目录。你想下载所有可用的zip文件或特定的zip文件吗?我在根目录中看到多个目录。我需要下载所有可用的zip文件每个目录001、002、003…中的所有zip文件?是的。我知道它们有很多文件(例如,所有加拿大的DEM模型。仅次于俄罗斯的第二大国家)好的,我也得到了同样的错误。你读过这篇文章吗:我会尽力让它对你的案子起作用。一旦它工作,您就可以遍历每个目录。