問題描述
我正在嘗試從 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()
在沒有用戶名和密碼的情況下進入站點(其中不是必需的),我不知道如何在 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
代碼如下:它會下載TEMP目錄下所有匹配*DEM*
的文件,按目錄排序.
Here's the code: it download all files matching *DEM*
in TEMP directory, sorting by directory.
這篇關于使用 Python 從包含給定字符串的 FTP 服務器下載文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!