2012-02-01 43 views
3

我有一個腳本,它工作正常,直到我爲用戶交互添加了一個文件查詢框。它不斷重複這個問題,並且只有通過使輸入/輸出文件相同來強制使下列命令(shutil.copy2)崩潰纔會退出。爲什麼?這將導致循環使用tkinter時無限循環

 else: 
     #print "Does not exist" 
     newMetaFile=FileNm+"_BaseMetadata.xml" 
     root = Tkinter.Tk() 
     file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a xml base file to match with: '+File) 
     if file != None: 
      metafile=os.path.abspath(file.name) 
      file.close() 
      print metafile 
      shutil.copy2(metafile,newMetaFile) 
      print "copied" 
     else: 
     shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile) 

    tree=et.parse(newMetaFile)   
    print "Processing: "+str(File) 
的工作代碼

休息

import os, xml, arcpy, shutil, datetime, Tkinter,tkFileDialog 
from xml.etree import ElementTree as et 

path=os.getcwd() 
RootDirectory=path 
arcpy.env.workspace = path 
Count=0 

Generated_XMLs=RootDirectory+'\GeneratedXML_LOG.txt' 
f = open(Generated_XMLs, 'a') 
f.write("Log of Metadata Creation Process - Update: "+str(datetime.datetime.now())+"\n") 
f.close() 

for root, dirs, files in os.walk(RootDirectory, topdown=False): 
    #print root, dirs 
    for directory in dirs: 
     currentPath=os.path.join(root,directory) 
     os.chdir(currentPath) 
     arcpy.env.workspace = currentPath 
     print currentPath 
#def Create_xml(currentPath): 

     FileList = arcpy.ListFeatureClasses() 
     zone="_Zone" 

     for File in FileList: 
      Count+=1 
      FileDesc_obj = arcpy.Describe(File) 
      FileNm=FileDesc_obj.file 
      print FileNm 

      check_meta=os.listdir(currentPath) 
      existingXML=FileNm[:FileNm.find('.')] 
      existingExtension=FileNm[FileNm.find('.'):] 
      print "XML: "+existingXML 
      print check_meta 
      #if existingXML+'.xml' in check_meta: 
      for f in check_meta: 
      if f.startswith(existingXML) and f.endswith('.xml'): 
       print "exists, file name:", f 
       newMetaFile=FileNm+"_2012Metadata.xml" 
       shutil.copy2(f, newMetaFile) 

代碼

  for node in tree.findall('.//title'): 
       node.text = str(FileNm) 
      for node in tree.findall('.//northbc'): 
       node.text = str(FileDesc_obj.extent.YMax) 
      for node in tree.findall('.//southbc'): 
       node.text = str(FileDesc_obj.extent.YMin) 
      for node in tree.findall('.//westbc'): 
       node.text = str(FileDesc_obj.extent.XMin) 
      for node in tree.findall('.//eastbc'): 
       node.text = str(FileDesc_obj.extent.XMax)   
      for node in tree.findall('.//native/nondig/formname'): 
       node.text = str(os.getcwd()+"\\"+File) 
      for node in tree.findall('.//native/digform/formname'): 
       node.text = str(FileDesc_obj.featureType) 
      for node in tree.findall('.//avlform/nondig/formname'): 
       node.text = str(FileDesc_obj.extension) 
      for node in tree.findall('.//avlform/digform/formname'): 
       node.text = str(float(os.path.getsize(File))/int(1024))+" KB" 
      for node in tree.findall('.//theme'): 
       node.text = str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode)) 
      print node.text 
      projection_info=[] 
      Zone=FileDesc_obj.spatialReference.name 

      if "GCS" in str(FileDesc_obj.spatialReference.name): 
       projection_info=[FileDesc_obj.spatialReference.GCSName, FileDesc_obj.spatialReference.angularUnitName, FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName] 
       print "Geographic Coordinate system" 
      else: 
       projection_info=[FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName, FileDesc_obj.spatialReference.angularUnitName, Zone[Zone.rfind(zone)-3:]] 
       print "Projected Coordinate system" 
      x=0 
      for node in tree.findall('.//spdom'): 
       for node2 in node.findall('.//keyword'): 
        print node2.text 
        node2.text = str(projection_info[x]) 
        print node2.text 
        x=x+1 


      tree.write(newMetaFile) 

      f = open(Generated_XMLs, 'a') 
      f.write(str(Count)+": "+File+"; "+newMetaFile+"; "+currentPath+"\n") 
      f.close() 



    #  Create_xml(currentPath) 

回答

0

您發佈的代碼有之後的第二否則縮進錯誤,但有沒有循環,除非一個你迭代的列表非常非常大。這可能是很多複製操作花費時間(分析將有所幫助)。另外,在Python和英語中避免雙重否定是可取的; 如果文件!=無: 變成如果文件:

+0

謝謝。修正它通過添加一個break語句,但現在我得到其他問題-http://stackoverflow.com/questions/9090111/attributeerror-len-error-with-ntpath-py-an-tkinter-py我不明白因爲os.path.join到現在還沒有任何問題。 – GeorgeC 2012-02-01 04:43:42

-2

必須添加一個break語句以使其脫離循環。

+0

請不要在答案部分提供更新 - 這不是論壇。如果你想提供一個_complete_答案來顯示你做了什麼,那很好。但是,單行摘要並不能真正幫助任何可能會遇到同樣問題的人。 – 2014-01-07 23:03:21

0

這是你的循環。

else: 
     #print "Does not exist" 
     newMetaFile=FileNm+"_BaseMetadata.xml" 
     = Tkinter.Tk() 
     file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a xml base file to match with: '+File) 
     if file != None: 
      =os.path.abspath(file.name) 
      file.close()  
      print metafile 
      shutil.copy2(metafile,newMetaFile) 
      print "copied" 
     else: 
    shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile) 

    tree=et.parse(newMetaFile)   
    print "Processing: "+str(File) 

在此間舉行的最後,你想說:

break 

這將結束循環

或者你可以使用這個(例子)

x=0 
for y in z: 
    x+=1 
if x == 20: 
    break 

即會運行循環20次 哦,第二個其他的你需要縮進。