使用tkinter时无休止循环

2024-07-05 14:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它工作得很好,直到我添加了一个文件查询框,用于用户交互。它不断重复这个问题,只有在以下命令出现时才会退出(shutil.copy2号文件)通过使输入/输出文件相同而被迫崩溃。为什么?在

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)

导致循环的代码

^{pr2}$

其余工作代码

            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)

Tags: textinnodetreeobjforosxml
3条回答

这是你的循环。在

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)  

最后你想说:

^{pr2}$

这就结束了循环

或者你可以用这个(例子)

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

这将运行循环20次 哦,在第二次之后你需要缩进。在

您发布的代码在第二个else之后有一个缩进错误,但是没有循环,除非您迭代的列表之一非常非常大。这可能需要大量的复制操作(分析将有所帮助)。在

另外,在Python和英语中避免双重否定是可取的; 如果文件!=无: 如果文件:

必须添加一个break语句,这样它就脱离了循环。在

相关问题 更多 >