【问题标题】:Python pathlib glob function fails on WindowsError: [123]?Python pathlib glob 函数在 WindowsError 上失败:[123]?
【发布时间】:2017-01-25 12:54:01
【问题描述】:

我编写了以下 python 函数,该函数返回文件地理数据库路径的 python 列表。请注意,input_folder 是一个原始字符串,不包含 unicode 字符。

try:
    gdbs = list(Path(input_folder).glob('**/*.gdb'))
    for gdb in gdbs:
        print(gdb)
except WindowsError, e:
    print("error")

我遇到的问题是 pathlib glob 方法在目录中的文件路径中遇到 unicode 字符时失败。

我尝试了以下方法,但仍然失败,我认为这是因为我没有转换 glob 生成器遇到的路径。

try:
    gdbs = list(Path(unicode(input_folder)).glob('**/*.gdb'))
    for gdb in gdbs:
        print(gdb)
except WindowsError, e:
    print("error")

返回的错误信息是:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'R:\\Data\\Africa\\Tanzania\\fromDropbox\\DART\\BRT Phase 2-3 designs\\1.12 Engineering Drawings for Service\\ROAD LIGHT\\PDF\\01.Traffic Sign(Kilwa)-??04.pdf'

任何处理以下错误的帮助将不胜感激。

【问题讨论】:

    标签: python unicode windowserror


    【解决方案1】:

    试试这个:

    input_folder = r'R:\Data\Africa\Tanzania\fromDropbox\DART\BRT Phase 2-3 designs\1.12 Engineering Drawings for Service\ROAD LIGHT\PDF\01.Traffic Sign(Kilwa)-??04.pdf'
    

    正确的调用应该在路径前面有“r”,并使用单斜杠。

    【讨论】:

    • 如前所述,路径是原始字符串,因此您的建议无济于事。
    【解决方案2】:

    这似乎是 pathlib 的问题,因为 Python 2.7 无法处理非 ascii 字符。 pathlib chokes up on international characters on Python 2 on Windows

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 1970-01-01
      • 2019-09-12
      • 2019-03-30
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多