Python:为什么我收到一个AttributeError:__ enter__

问题描述 投票:2回答:1

我没有重新分配open关键字但仍然收到此错误。有任何建议或方向来修复我的错误?

 with tempfile.mkdtemp() as test_dir:
        print(test_dir)

AttributeError: __enter__

我也是python的新手,我很难理解这些概念。

python attributeerror contextmanager temporary-directory
1个回答
5
投票

你错误地使用mkdtemp。 mkdtemp将路径名称返回为str,而不是上下文管理器。

如果您需要上下文管理器来管理临时目录,则需要使用TemporaryDirectory,它可以从Python 3.2及更高版本获得。

© www.soinside.com 2019 - 2024. All rights reserved.