Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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 对象在使用ElementTree时没有属性错误_Python_Elementtree - Fatal编程技术网

Python 对象在使用ElementTree时没有属性错误

Python 对象在使用ElementTree时没有属性错误,python,elementtree,Python,Elementtree,我是python新手,有以下问题。如果我取消注释此代码的最后一行: # traverse all directories for root, dirs, files in os.walk(args.rootfolder): for file in files: print('Current file name: ' + file) if file.endswith('.savx'): # read single xml savx f

我是python新手,有以下问题。如果我取消注释此代码的最后一行:

# traverse all directories
for root, dirs, files in os.walk(args.rootfolder):
    for file in files:
        print('Current file name: ' + file)
        if file.endswith('.savx'):
            # read single xml savx file
            currfilename = os.path.join(root,file)
            print('Current full file name: ' + currfilename)
            tree = ET.parse(currfilename)
            # root = tree.getroot() <-- if I uncomment this line I get errors
#遍历所有目录
对于os.walk(args.rootfolder)中的root、dirs和文件:
对于文件中的文件:
打印('当前文件名:'+文件)
如果文件.endswith('.savx'):
#读取单个xml savx文件
currfilename=os.path.join(根目录,文件)
打印('当前完整文件名:'+currfilename)
tree=ET.parse(currfilename)

#root=tree.getroot()您在代码中使用了两次
root
,用于不同的用途:

for root, dirs, files in os.walk(args.rootfolder):
#   ^^^^

因此,当您尝试在构建下一个要加载的文件名时将
用作路径字符串时:

currfilename = os.path.join(root,file)
你会发现你用一个ElementTree对象替换了
根路径名

为根目录名或ElementTree对象使用不同的名称。使用
dirname
例如:

for dirname, dirs, files in os.walk(args.rootfolder):
    for file in files:
        print('Current file name: ' + file)
        if file.endswith('.savx'):
            # read single xml savx file
            currfilename = os.path.join(dirname, file)
            print('Current full file name: ' + currfilename)
            tree = ET.parse(currfilename)
            root = tree.getroot()

您在代码中使用了两次
root
,用于不同的用途:

for root, dirs, files in os.walk(args.rootfolder):
#   ^^^^

因此,当您尝试在构建下一个要加载的文件名时将
用作路径字符串时:

currfilename = os.path.join(root,file)
你会发现你用一个ElementTree对象替换了
根路径名

为根目录名或ElementTree对象使用不同的名称。使用
dirname
例如:

for dirname, dirs, files in os.walk(args.rootfolder):
    for file in files:
        print('Current file name: ' + file)
        if file.endswith('.savx'):
            # read single xml savx file
            currfilename = os.path.join(dirname, file)
            print('Current full file name: ' + currfilename)
            tree = ET.parse(currfilename)
            root = tree.getroot()

您在代码中使用了两次
root
,用于不同的用途:

for root, dirs, files in os.walk(args.rootfolder):
#   ^^^^

因此,当您尝试在构建下一个要加载的文件名时将
用作路径字符串时:

currfilename = os.path.join(root,file)
你会发现你用一个ElementTree对象替换了
根路径名

为根目录名或ElementTree对象使用不同的名称。使用
dirname
例如:

for dirname, dirs, files in os.walk(args.rootfolder):
    for file in files:
        print('Current file name: ' + file)
        if file.endswith('.savx'):
            # read single xml savx file
            currfilename = os.path.join(dirname, file)
            print('Current full file name: ' + currfilename)
            tree = ET.parse(currfilename)
            root = tree.getroot()

您在代码中使用了两次
root
,用于不同的用途:

for root, dirs, files in os.walk(args.rootfolder):
#   ^^^^

因此,当您尝试在构建下一个要加载的文件名时将
用作路径字符串时:

currfilename = os.path.join(root,file)
你会发现你用一个ElementTree对象替换了
根路径名

为根目录名或ElementTree对象使用不同的名称。使用
dirname
例如:

for dirname, dirs, files in os.walk(args.rootfolder):
    for file in files:
        print('Current file name: ' + file)
        if file.endswith('.savx'):
            # read single xml savx file
            currfilename = os.path.join(dirname, file)
            print('Current full file name: ' + currfilename)
            tree = ET.parse(currfilename)
            root = tree.getroot()