目录列表到xml-无法连接'str'和'NoneType'对象-如何处理?

胡椒博士

在这里我发现使用一部分脚本在Windows PC上的目录中运行以生成XML文件。但是,我遇到了以上错误,并且不确定如何处理该错误。我添加了一个try / except,但是它仍然崩溃了。如果我将目录“ DirTree3(“ C:/”)”替换为“ DirTree3((os.getcwd())”,将目录设置为当前工作目录,则此方法非常有效

def DirTree3(path):
    try:
        result = '<dir>%s\n' % xml_quoteattr(os.path.basename(path))
        for item in os.listdir(path):
            itempath = os.path.join(path, item)
            if os.path.isdir(itempath):
                result += '\n'.join('  ' + line for line in
                                    DirTree3(os.path.join(path, item)).split('\n'))
            elif os.path.isfile(itempath):
                result += '  <file> %s </file>\n' % xml_quoteattr(item)
        result += '</dir> \n'
        return result
    except Exception:
        pass


print '<DirectoryListing>\n' + DirTree3("C:/") + '\n</DirectoryListing>'

附带说明,此脚本将在没有管理员特权的系统上运行,因此不能以admin身份运行

马蒂诺

根据您在下面有关获取和希望忽略任何路径访问错误的意见,我在下面的答案中修改了代码,以尽最大努力做到这一点。请注意,如果发生其他类型的异常,它将仍然终止。

def DirTree3(path):
    try:
        result = '<dir>%s\n' % xml_quoteattr(os.path.basename(path))
        try:
            items = os.listdir(path)
        except WindowsError as exc:
            return '<error> {} </error>'.format(xml_quoteattr(str(exc)))

        for item in items:
            itempath = os.path.join(path, item)
            if os.path.isdir(itempath):
                result += '\n'.join('  ' + line for line in
                                    DirTree3(os.path.join(path, item)).split('\n'))
            elif os.path.isfile(itempath):
                result += '  <file> %s </file>\n' % xml_quoteattr(item)
        result += '</dir> \n'
        return result
    except Exception as exc:
        print('exception occurred: {}'.format(exc))
        raise

print '<DirectoryListing>\n' + DirTree3("C:/") + '\n</DirectoryListing>'

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

re.search()TypeError:无法连接“ str”和“ NoneType”对象

来自分类Dev

尝试连接“ str”和“ NoneType”对象时出现TypeError

来自分类Dev

TypeError:无法连接'str'和'NoneType'对象python bs4

来自分类Dev

无法连接'str'和'list'对象列表包含整数

来自分类Dev

TypeError:将自定义网址放入scrapy.Request()时,无法连接“ str”和“ NoneType”对象

来自分类Dev

TypeError:无法连接“ str”和“ dict”对象

来自分类Dev

openerp无法连接“ str”和“ function”对象

来自分类常见问题

如何连接str和int对象?

来自分类Dev

plt.bar-> TypeError:无法连接“ str”和“ float”对象

来自分类Dev

Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

来自分类Dev

TypeError:无法连接“ str”和“ pygame.Surface”对象

来自分类Dev

TypeError:无法连接'str'和'function'对象的python文件

来自分类Dev

rpsls游戏“(python)错误:无法连接'str'和'int'对象

来自分类Dev

Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

来自分类Dev

类型错误无法连接“ str”和“ ErrorList”对象

来自分类Dev

abaqus脚本:TypeError:无法连接“ str”和“ Set”对象

来自分类Dev

TypeError:无法连接“ str”和“ pygame.Surface”对象

来自分类Dev

TypeError:无法连接'str'和'function'对象的python文件

来自分类Dev

Python TypeError:无法连接“ str”和“ list”对象

来自分类Dev

Python Selenium TypeError:无法连接“ str”和“ WebElement”对象

来自分类Dev

TypeError:无法连接“ str”和“ float”对象:pandas

来自分类Dev

类型错误:无法连接“str”和“int”对象 1

来自分类Dev

'str'对象不可调用,如何处理?

来自分类Dev

'str'对象不可调用,如何处理?

来自分类Dev

Scala:如何处理列表中的对象

来自分类Dev

我如何解决TypeError:使用flask和Sqlite3时'NoneType'对象无法下标?

来自分类Dev

Python:切片:TypeError:无法连接“ str”和“ int”对象

来自分类Dev

TypeError:无法连接电子邮件中的“ str”和“ list”对象

来自分类Dev

TypeError:无法连接类型为“ <class'str'>”的对象;仅Series和DataFrame objs有效

Related 相关文章

  1. 1

    re.search()TypeError:无法连接“ str”和“ NoneType”对象

  2. 2

    尝试连接“ str”和“ NoneType”对象时出现TypeError

  3. 3

    TypeError:无法连接'str'和'NoneType'对象python bs4

  4. 4

    无法连接'str'和'list'对象列表包含整数

  5. 5

    TypeError:将自定义网址放入scrapy.Request()时,无法连接“ str”和“ NoneType”对象

  6. 6

    TypeError:无法连接“ str”和“ dict”对象

  7. 7

    openerp无法连接“ str”和“ function”对象

  8. 8

    如何连接str和int对象?

  9. 9

    plt.bar-> TypeError:无法连接“ str”和“ float”对象

  10. 10

    Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

  11. 11

    TypeError:无法连接“ str”和“ pygame.Surface”对象

  12. 12

    TypeError:无法连接'str'和'function'对象的python文件

  13. 13

    rpsls游戏“(python)错误:无法连接'str'和'int'对象

  14. 14

    Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

  15. 15

    类型错误无法连接“ str”和“ ErrorList”对象

  16. 16

    abaqus脚本:TypeError:无法连接“ str”和“ Set”对象

  17. 17

    TypeError:无法连接“ str”和“ pygame.Surface”对象

  18. 18

    TypeError:无法连接'str'和'function'对象的python文件

  19. 19

    Python TypeError:无法连接“ str”和“ list”对象

  20. 20

    Python Selenium TypeError:无法连接“ str”和“ WebElement”对象

  21. 21

    TypeError:无法连接“ str”和“ float”对象:pandas

  22. 22

    类型错误:无法连接“str”和“int”对象 1

  23. 23

    'str'对象不可调用,如何处理?

  24. 24

    'str'对象不可调用,如何处理?

  25. 25

    Scala:如何处理列表中的对象

  26. 26

    我如何解决TypeError:使用flask和Sqlite3时'NoneType'对象无法下标?

  27. 27

    Python:切片:TypeError:无法连接“ str”和“ int”对象

  28. 28

    TypeError:无法连接电子邮件中的“ str”和“ list”对象

  29. 29

    TypeError:无法连接类型为“ <class'str'>”的对象;仅Series和DataFrame objs有效

热门标签

归档