Python try / except 似乎不适用于 KeyError

Python try / except does not seem to work with KeyError

虽然我已经使用了 try/except 子句数百次,但我以前从未见过这样的行为,现在我感到很困惑。我可以知道我错过了什么吗?

import pandas as pd
import os


season_ep = {}
for season in range(1, 16):
    for ind, row in pd.read_html('https://thetvdb.com/series/curious-george/seasons/official/%d' % season)[0].iterrows():
        season_ep[row['Name']] = row['Unnamed: 0']

errors = []
for root, dirs, files in os.walk("/run/user/1000/gvfs/smb-share:server=nas.local,share=media/TV Shows/Curious George", topdown=False):
    for name in files:
        try:
            to_print = season_ep[name.split('-')[-1][:-4]]
            print(to_print)
        except KeyError:
            errors.append(os.path.join(dirs, name))

for e in errors:
    print('ERROR: %s' % e)

基本上,我正在字典 season_ep 中搜索键 'Old McGeorgie Had a Farm' which returns a KeyError,但我不明白为什么我收到以下错误。我希望 except 部分识别 KeyError 并接管,允许代码完全执行。

Traceback (most recent call last):
  File "/home/jthom/PycharmProjects/TVDB_ID/main.py", line 14, in <module>
    to_print = season_ep[name.split('-')[-1][:-4]]
KeyError: 'Old McGeorgie Had a Farm'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/snap/pycharm-community/278/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/snap/pycharm-community/278/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/jthom/PycharmProjects/TVDB_ID/main.py", line 17, in <module>
    errors.append(os.path.join(dirs, name))
  File "/usr/lib/python3.10/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not list

我 运行 Python 3.10.4 Ubuntu 22.04。感谢您的帮助!

如果仔细查看堆栈跟踪,您会看到以下片段:During handling of the above exception, another exception occurred。这是告诉你上面的 KeyError 被捕获了,但是在 execpt 块中引发了另一个异常。

解决此问题的最简单方法是添加嵌套的 try-except:

        try:
            to_print = season_ep[name.split('-')[-1][:-4]]
            print(to_print)
        except KeyError:
            try:
                errors.append(os.path.join(dirs, name))
            except TypeError:
                # handle this error somehow
                pass

这里问题的根源在于您将列表传递给 os.path.join expecting a str, bytes or os.PathLike object, not list. My guess is you want to join the root value rather than dirs. You can see something similar in an example for the docs for os.walk:

# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

总而言之,您的路径连接代码应如下所示:

                errors.append(os.path.join(root, name))

如需收集错误位置列表,只需追加路径名root和文件名name

    try:
        to_print = season_ep[name.split('-')[-1][:-4]]
        print(to_print)
    except KeyError:
        #errors.append(os.path.join(dirs, name))
        errors.append(os.path.join(root, name))