久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Python 從包含給定字符串的 FTP 服務(wù)器下載文

Download files from an FTP server containing given string using Python(使用 Python 從包含給定字符串的 FTP 服務(wù)器下載文件)
本文介紹了使用 Python 從包含給定字符串的 FTP 服務(wù)器下載文件的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試從 FTP 服務(wù)器下載大量共享公共字符串 (DEM) 的文件.這些文件嵌套在多個(gè)目錄中.例如,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 服務(wù)器位于此處:ftp://ftp.igsb.uiowa.edu/gis_library/counties/,不需要用戶名和密碼.所以,我想遍歷每個(gè)縣并下載包含字符串 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.

我在這里閱讀了很多關(guān)于 Stack Overflow 的問題和 Python 的文檔,但無法弄清楚如何使用 ftplib.FTP() 在沒有用戶名和密碼的情況下進(jìn)入站點(diǎn)(其中不是必需的),我不知道如何在 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.

提前感謝您的幫助

推薦答案

好的,好像可以了.如果嘗試下載目錄或掃描文件,可能會(huì)出現(xiàn)問題.異常處理可以方便地捕獲錯(cuò)誤的文件類型并跳過.

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 無法工作,因?yàn)槟谶h(yuǎn)程文件系統(tǒng)上,但您可以使用 fnmatch 來匹配名稱

glob.glob cannot work since you're on a remote filesystem, but you can use fnmatch to match the names

代碼如下:它會(huì)下載TEMP目錄下所有匹配*DEM*的文件,按目錄排序.

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 + " ...
")
    #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+" ...
")
            local_filename = os.path.join(local_dir,f)
            with open(local_filename, 'wb') as fh:
                fc.retrbinary('RETR '+ l + "/" + f, fh.write)

fc.close()

這篇關(guān)于使用 Python 從包含給定字符串的 FTP 服務(wù)器下載文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動(dòng)對象不可下標(biāo))
主站蜘蛛池模板: 日韩中文视频 | 免费一区二区三区 | 久久久久网站 | 精品欧美一区二区三区久久久小说 | 视频一区二区在线观看 | 欧美1—12sexvideos | 国产精品一区久久久 | 久久国产精品一区二区 | 国产电影一区 | 久久福利电影 | 久久精品国产免费 | 国产1区2区3区 | av免费在线观看网站 | 狠狠操狠狠干 | 亚洲国产成人在线视频 | 久久精品欧美视频 | 综合第一页| 精品免费国产一区二区三区四区介绍 | 97精品久久 | 日本涩涩网 | 人人99 | 成人午夜激情 | 国产欧美精品一区二区三区 | 手机看片169 | 欧美天堂在线观看 | 男人视频网站 | 欧美激情精品久久久久久变态 | 国产精品美女久久久久久免费 | 亚洲免费一区 | 日韩午夜| 在线观看中文字幕亚洲 | 国产精品一区在线观看 | 久久精品中文 | 欧美日韩在线一区二区 | 国产精品久久久久久久三级 | 亚洲www啪成人一区二区麻豆 | 夜夜骚 | 一级欧美黄色片 | 日韩精品在线免费 | 一区二区三区播放 | 国产精品一区二区av |