Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中为每个循环更新一个_Python_Gis_Arcgis - Fatal编程技术网

在Python中为每个循环更新一个

在Python中为每个循环更新一个,python,gis,arcgis,Python,Gis,Arcgis,下面的python代码获取一个文件列表,并将其压缩。我需要的唯一文件地理数据库(基于文件的数据库)称为“数据”,因此如何修改循环以仅包含称为数据的基于文件的数据库?更具体地说,文件地理数据库存储为系统文件夹,其中包含存储和管理空间数据的二进制文件。所以我需要整个系统文件夹Data.gdb 非常感谢 #********************************************************************** # Description: # Zips the

下面的python代码获取一个文件列表,并将其压缩。我需要的唯一文件地理数据库(基于文件的数据库)称为“数据”,因此如何修改循环以仅包含称为数据的基于文件的数据库?更具体地说,文件地理数据库存储为系统文件夹,其中包含存储和管理空间数据的二进制文件。所以我需要整个系统文件夹Data.gdb

非常感谢

#**********************************************************************
# Description:
#    Zips the contents of a folder, file geodatabase or ArcInfo workspace
#    containing coverages into a zip file.
# Parameters:
#   0 - Input workspace
#   1 - Output zip file. It is assumed that the caller (such as the
#       script tool) added the .zip extension.
#
#**********************************************************************

# Import modules and create the geoprocessor
import sys, zipfile, arcgisscripting, os, traceback
gp = arcgisscripting.create()

# Function for zipping files 
def zipws(path, zip):
    isdir = os.path.isdir

    # Check the contents of the workspace, if it the current
    # item is a directory, gets its contents and write them to
    # the zip file, otherwise write the current file item to the
    # zip file
    #
    for each in os.listdir(path):
        fullname = path + "/" + each
        if not isdir(fullname):
            # If the workspace is a file geodatabase, avoid writing out lock
            # files as they are unnecessary
            #
            if not each.endswith('.lock'):
                # gp.AddMessage("Adding " + each + " ...")
                # Write out the file and give it a relative archive path
                #
                try: zip.write(fullname, each)
                except IOError: None # Ignore any errors in writing file
        else:
            # Branch for sub-directories
            #
            for eachfile in os.listdir(fullname):
                if not isdir(eachfile):
                    if not each.endswith('.lock'):
                        # gp.AddMessage("Adding " + eachfile + " ...")
                        # Write out the file and give it a relative archive path
                        #
                        try: zip.write(fullname + "/" + eachfile, \
                                       os.path.basename(fullname) + "/" + eachfile)
                        except IOError: None # Ignore any errors in writing file


if __name__ == '__main__':
    try:
        # Get the tool parameter values
        #
        inworkspace = sys.argv[1]
        outfile = sys.argv[2]     

        # Create the zip file for writing compressed data
        #
        zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_DEFLATED)
        zipws(inworkspace, zip)
        zip.close()

        # Set the output derived parameter value for models
        #
        gp.setparameterastext(1, outfile)
        gp.AddMessage("Zip file created successfully")

    except:
        # Return any python specific errors as well as any errors from the geoprocessor
        #
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
                str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
        gp.AddError(pymsg)

        msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"
        gp.AddError(msgs)

遍历目录树的最佳方法是——为您执行文件/目录分离,并为您执行向下递归到子目录的操作

因此:

我不确定我是否已经捕获了您想要确定
zip.write
的两个参数的全部逻辑(从您的代码中我看不清楚),但是,如果没有,这应该很容易调整

另外,我不确定您是否希望
在结尾返回
:效果是只压缩一个以这种方式命名的文件,而不是压缩树中(在各自的子目录中)所有以这种方式命名的文件。如果您知道只有一个这样的文件,那么最好将
return
保留在中(这样只会加快速度)。如果要在有多个文件时使用所有此类文件,请删除
返回项

编辑:OP想要的“一件事”是一个目录,而不是一个文件。在这种情况下,作为最简单的解决办法,我建议:

def zipws(path, zip, dirname='Data.gdb'):
  for root, dirs, files in os.walk(path):
    if os.path.basename(root) != dirname: continue
    for filename in files:
      zip.write(os.path.join(root, filename),
                os.path.join(dirname, filename))
    return

同样,通过一个类似的猜测,你想用什么作为你的存档名,这是一个谜。

浏览目录树的最佳方式是——为你进行文件/目录分离,也为你进行递归到子目录

因此:

我不确定我是否已经捕获了您想要确定
zip.write
的两个参数的全部逻辑(从您的代码中我看不清楚),但是,如果没有,这应该很容易调整

另外,我不确定您是否希望
在结尾返回
:效果是只压缩一个以这种方式命名的文件,而不是压缩树中(在各自的子目录中)所有以这种方式命名的文件。如果您知道只有一个这样的文件,那么最好将
return
保留在中(这样只会加快速度)。如果要在有多个文件时使用所有此类文件,请删除
返回项

编辑:OP想要的“一件事”是一个目录,而不是一个文件。在这种情况下,作为最简单的解决办法,我建议:

def zipws(path, zip, dirname='Data.gdb'):
  for root, dirs, files in os.walk(path):
    if os.path.basename(root) != dirname: continue
    for filename in files:
      zip.write(os.path.join(root, filename),
                os.path.join(dirname, filename))
    return
同样,通过一个类似的猜测,我们可以知道,您想将什么确切地用作您的存档名称。

从这一行开始:

    zipws(inworkspace, zip)
您不希望使用此函数从多个文件构建zip文件。 看起来您想要构建一个只包含一个成员的zip文件

换成这个

     try: 
         zip.write(os.path.join('.', 'Data.gdb'))
     except IOError: 
         pass # Ignore any errors in writing file
扔掉你显然不想使用的
zipws
函数

阅读此文,可能会有所帮助:

从这一行开始:

    zipws(inworkspace, zip)
您不希望使用此函数从多个文件构建zip文件。 看起来您想要构建一个只包含一个成员的zip文件

换成这个

     try: 
         zip.write(os.path.join('.', 'Data.gdb'))
     except IOError: 
         pass # Ignore any errors in writing file
扔掉你显然不想使用的
zipws
函数


阅读本文,可能会有所帮助:

No,Data.gdb是基于文件的数据库的名称,这是我想要压缩的唯一文件。我不想在zip文件中包含的所有其他文件。如果'Data.gdb'是一个目录,那么这应该是该程序的命令行参数,您就完成了。不需要编程。不,Data.gdb是基于文件的数据库的名称,这是我想要压缩的唯一文件。我不想在zip文件中包含的所有其他文件。如果'Data.gdb'是一个目录,那么这应该是该程序的命令行参数,您就完成了。无需编程。Data.gdb是多个文件,因为它是基于文件的数据库。那么这会成为一个问题吗?@Josh:你应该更新你的问题,使其在这一点上非常具体。不清楚这个名字是什么意思。是“目录”吗?还是一个“文件”?请实际更新您的问题以提供缺少的信息。Data.gdb是多个文件,因为它是基于文件的数据库。那么这会成为一个问题吗?@Josh:你应该更新你的问题,使其在这一点上非常具体。不清楚这个名字是什么意思。是“目录”吗?还是一个“文件”?请实际更新您的问题以提供缺少的信息。嗨,Alex-我更新了上面的问题,以便提供关于Data.gdb(它是一个包含多个二进制文件的系统文件夹)的更多详细信息。此外,我还注释了我的整个zipws函数,添加了您的代码,并将zipws(inworkspace,zip)更新为zipws(inworkspace,zip,filename),但当我运行它时,出现了语法错误。想法?@Josh,你是后者,我只是打了一个封闭的模板——编辑以修复。如果你要找的是一个目录,而不是一个文件,那么最简单的IMHO就是查找根目录的basename——因为我正在编辑,所以我会显示新版本。嗨,Alex——我更新了我上面的问题,以便提供关于Data.gdb(它是一个包含多个二进制文件的系统文件夹)的更多详细信息。此外,我还注释了我的整个zipws函数,添加了您的代码,并将zipws(inworkspace,zip)更新为zipws(inworkspace,zip,filename),但当我运行它时,出现了语法错误。想法?@Josh,你是后者,我只是打了一个封闭的模板——编辑以修复。如果您要查找的是一个目录,而不是一个文件,那么最简单的IMHO就是查找root的basename——因为我正在编辑,所以我将显示新版本。