2010-08-25 112 views
1

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

非常感谢

#********************************************************************** 
# 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) 
+0

没有,Data.gdb是基于文件的数据库的名称,这是我想拉上的唯一文件。我不希望包含在zip文件中的所有其他文件。 – Josh 2010-08-25 20:29:20

+0

如果'Data.gdb'是一个目录,那么这应该是这个程序的命令行参数,并且你完成了。无需编程。 – 2010-08-25 21:35:42

回答

1

走在目录树的最佳方式是os.walk - 确实的文件/目录分离的你,也不会递归到子目录为您服务。

所以:

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

我不能肯定我已经捕捉到了与您要确定两个参数zip.write(这不是明显,我从你的代码)的整个逻辑,但是,如果不,这应该很容易调整。

而且,我不知道,如果你想要一个return末:效果荏苒只一个名为这样的文件,而不是压缩和解所有文件命名树可能出现这样(在他们各自的子目录中)。如果你知道只有一个这样的文件,不妨留下return(它只会加速一点点)。如果您想在有多个文件时使用所有这些文件,请删除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 

有类似的猜测WRT的究竟是的是,你要使用你的存档名称总谜:在这种情况下,我建议,作为最简单的解决方案。

+0

嗨亚历克斯,我更新了我的问题上面,以提供Data.gdb(这是一个多个二进制文件的系统文件夹)的更多细节。此外,我评论了我的整个zipws函数,并添加了代码,并且还更新了zipws(inworkspace,zip)为zipws(inworkspace,zip,filename),但是当我运行它时,出现语法错误。思考? – Josh 2010-08-25 21:51:37

+0

@Josh,对于后者,我只是错过了一个封闭的paren编辑来修复。如果你正在寻找的是一个目录,而不是一个文件,最简单的恕我直言就是寻找根的基本名称 - 因为我编辑无论如何,我会展示新版本。 – 2010-08-26 00:05:32

+0

不错的工作亚历克斯。这工作。 – Josh 2010-08-26 11:52:38

0

开始在这一行:

zipws(inworkspace, zip) 

你不想使用此此功能来构建从多个文件的zip文件。 看来你只想用一个成员创建一个zip文件。

用此代替。

 try: 
     zip.write(os.path.join('.', 'Data.gdb')) 
    except IOError: 
     pass # Ignore any errors in writing file 

丢掉你显然不想使用的zipws函数。

看了这个,它可以帮助:http://docs.python.org/library/zipfile.html

+0

Data.gdb是多个文件,因为它是基于文件的数据库。那会是一个问题吗? – Josh 2010-08-25 21:32:12

+0

@Josh:你应该**更新**你的问题,在这一点上非常具体。这个名字的含义并不清楚。它是一个“目录”吗?或者它是一个“文件”?请实际更新您的问题以提供缺少的信息。 – 2010-08-25 21:34:15

+0

已更新,谢谢 – Josh 2010-08-25 21:41:48