Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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/8/xslt/3.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_Elementtree - Fatal编程技术网

如何让python移动到目录中的下一个文件?

如何让python移动到目录中的下一个文件?,python,elementtree,Python,Elementtree,在检查文件扩展名是否正确并在元素树中对其进行解析之后,如何在Python中移动到下一个文件?请参阅下面的代码。在检查该文件的.bpmn并对其进行解析之后,我想移动到下一个文件,检查它是否为.xml,然后进行解析 path = 'path_to_directory' for filename in os.listdir(path): if filename.endswith(".bpmn"): fullname = os.path.join(path, filename)

在检查文件扩展名是否正确并在元素树中对其进行解析之后,如何在Python中移动到下一个文件?请参阅下面的代码。在检查该文件的.bpmn并对其进行解析之后,我想移动到下一个文件,检查它是否为.xml,然后进行解析

path = 'path_to_directory'
for filename in os.listdir(path):
    if filename.endswith(".bpmn"):
        fullname = os.path.join(path, filename)
        tree = ET.parse(fullname)
        root = tree.getroot()

     else:
         print("must end with .bpmn")

    if filename.endswith(".xml"):
        fullname = os.path.join(path, filename)
        treeXML = ET.parse(fullname)
        rootXML = treeXML.getroot()
     else:
        print("must end with xml")

for循环为您执行此操作,并继续绕过它。 Python中的continue语句将控件返回到while循环的开头。continue语句拒绝循环当前迭代中的所有剩余语句,并将控件移回循环顶部

continue语句可用于while和for循环。 对于os.listdir(路径)中的文件名:

所以,文件名在循环执行后会不断更改。 试试这个。 继续使用 路径='path\u to\u目录'

for filename in os.listdir(path):
    if filename.endswith(".bpmn"):
        fullname = os.path.join(path, filename)
        tree = ET.parse(fullname)
        root = tree.getroot()
        continue
     else:
         print("must end with .bpmn")

    if filename.endswith(".xml"):
        fullname = os.path.join(path, filename)
        treeXML = ET.parse(fullname)
        rootXML = treeXML.getroot()
        continue
     else:
        print("must end with xml")
继续

import os, sys

# Open a file
path = "/var/www/html/"
dirs = os.listdir( path )

# This would print all the files and directories
for file in dirs:
   print file

这就是循环的用途。它们将自动移动到源iterable中的下一项。