使用Python将ftp站点上的文件地理数据库复制到本地磁盘?


11

我想使用Python脚本下载位于ftp站点上的文件地理数据库。现在,我在考虑一种实现此目的的方法是将ftp地理数据库复制到计算机上的地理数据库。下面是我开始的脚本。有谁知道我可以如何更改此脚本,以便获得ftp gdb?谢谢


以下是基于提供的@om_hennners的最终工作代码。

import arcpy, os, sys
from arcpy import env
arcpy.env.overwriteOutput = True
from ftplib import FTP

directory = "/group/geodb" #location of gdb on ftp
folder = "D:\\temp\\" 
out_gdb = "data.gdb"
out_path = folder + os.sep + out_gdb
copy_gdb = "hydro.gdb" # This is the gdb I would like to copy from the ftp  site
ftp = FTP("10.4.2.22")
ftp.login("user", "pass")

ftp.cwd(os.path.join(directory, copy_gdb))
print "Changed to " + os.path.join(directory, copy_gdb)

filenames = ftp.nlst()
print filenames

print "starting to write"
for f in filenames:
    with open(os.path.join(out_path, f), 'wb') as local_file:
    ftp.retrbinary('RETR '+ f, local_file.write)      


ftp.close()
print "closed ftp connection"

除非我忽略了它,否则您是否在任何地方设置环境工作区?无论哪种方式,copy_gdb变量都将使用它作为其位置。
AHigh

4
您是否考虑过将地理数据库压缩为ZIP文件?几乎没有理由在FTP站点上具有未压缩的地理数据库。
blah238

是否可以在ftp站点上将地理数据库设置为工作空间而无需下载它?
geogeek

3
@geogeek不,不是...
blah238

1
@PattyJula今天必须写一个ftp脚本。事实证明,内置的ftplib对于导航目录层次结构是一种痛苦。相反,我是使用ftputil做到的,如果您要再次尝试的话,我会建议您这样做。
om_henners 2012年

Answers:


9

在这种情况下,您无需使用arcpy库来复制地理数据库。相反,您正在查看通过ftp连接复制文件,可以使用ftplib retrbinary命令进行复制。

另请注意,文件系统将地理数据库视为文件夹对象,其中包含一组文件。也就是说,它们不是可以使用ftplib一键式传输的单个二进制文件。

因此,实际上您要做的就是创建一个名为的本地文件夹data.gdb,然后在ftp服务器上循环遍历其中的所有文件hydro.gdb并下载它们。像下面这样的东西应该可以工作(用一些从此堆栈溢出答案中借来的代码,因为我不太了解ftplib):

import os
import os.path
from ftplib import FTP

directory = "/group/geodb" #location of gdb on ftp
copy_gdb = "hydro.gdb" # This is the gdb I would like to copy from the ftp site

folder = "D:\\temp\\"
out_gdb = "data.gdb"
out_path = os.path.join(folder, out_gdb)

#First, create the out geodatabase as a folder
os.mkdir(out_path)

#FTP logon
ftp = FTP("10.4.2.22")
ftp.login("user", "pass")

#Again, treat the gdb as a folder and navigate there
ftp.cwd(os.path.join(directory, copy_gdb))
print "Changed to " + os.path.join(directory, copy_gdb)

#Now get a list of all files in the folder
filenames = ftp.nlst()
print filenames

#and loop through the filenames to download the files to your local 'gdb'
for f in filenames:
    with open(os.path.join(out_path, f), 'wb') as local_file:
        ftp.retrbinary('RETR '+ filename, local_file.write)

ftp.close()
print "closed ftp connection"

1
那行得通。非常感谢om_henners!我必须用您的代码更改一两个小事情,我将很快发布最终脚本。
帕蒂·朱拉
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.