问题描述
我正在尝试从FTP服务器下载大量共享相同字符串(DEM
)的文件.这些文件嵌套在多个目录中.例如,Adair/DEM*
和Adams/DEM*
I'm trying to download a large number of files that all share a common string (DEM
) from an FTP sever. These files are nested inside multiple directories. For example, Adair/DEM*
and Adams/DEM*
FTP服务器位于以下位置:ftp://ftp.igsb.uiowa.edu/gis_library/counties/
,不需要用户名和密码.因此,我想遍历每个县并下载包含字符串DEM
的文件.
The FTP sever is located here: ftp://ftp.igsb.uiowa.edu/gis_library/counties/
and requires no username and password.So, I'd like to go through each county and download the files containing the string DEM
.
我在这里已经阅读了许多有关Stack Overflow和Python文档的问题,但是无法弄清楚如何使用ftplib.FTP()
在没有用户名和密码的情况下进入网站(这不是必需的),我可以t弄清楚如何在ftplib或urllib中进行grep或使用glob.glob
.
I've read many questions here on Stack Overflow and the documentation from Python, but cannot figure out how to use ftplib.FTP()
to get into the site without a username and password (which is not required), and I can't figure out how to grep or use glob.glob
inside of ftplib or urllib.
预先感谢您的帮助
推荐答案
好的,看来可以.如果尝试下载目录或扫描文件,可能会出现问题.可以使用异常处理来捕获错误的文件类型并跳过.
Ok, seems to work. There may be issues if trying to download a directory, or scan a file. Exception handling may come handy to trap wrong filetypes and skip.
glob.glob
无法使用,因为您使用的是远程文件系统,但是可以使用fnmatch
来匹配名称
glob.glob
cannot work since you're on a remote filesystem, but you can use fnmatch
to match the names
这是代码:它将所有与*DEM*
匹配的文件下载到TEMP目录中,并按目录排序.
Here's the code: it download all files matching *DEM*
in TEMP directory, sorting by directory.
import ftplib,sys,fnmatch,os
output_root = os.getenv("TEMP")
fc = ftplib.FTP("ftp.igsb.uiowa.edu")
fc.login()
fc.cwd("/gis_library/counties")
root_dirs = fc.nlst()
for l in root_dirs:
sys.stderr.write(l + " ...\n")
#print(fc.size(l))
dir_files = fc.nlst(l)
local_dir = os.path.join(output_root,l)
if not os.path.exists(local_dir):
os.mkdir(local_dir)
for f in dir_files:
if fnmatch.fnmatch(f,"*DEM*"): # cannot use glob.glob
sys.stderr.write("downloading "+l+"/"+f+" ...\n")
local_filename = os.path.join(local_dir,f)
with open(local_filename, 'wb') as fh:
fc.retrbinary('RETR '+ l + "/" + f, fh.write)
fc.close()
这篇关于使用Python从包含给定字符串的FTP服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!