2010-08-25 63 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