Python:为什么我收到AttributeError:__enter__ [英] Python: Why am I receiving an AttributeError: __enter__

查看:114
本文介绍了Python:为什么我收到AttributeError:__enter__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有重新分配open关键字,但仍然收到此错误.有任何建议或指导来纠正我的错误吗?

I am not reassigning the open keyword yet still receive this error. Any suggestions or direction to fix my error?

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

AttributeError:__enter __

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

I am also new to python and I am having a hard time understanding these concepts.

推荐答案

您使用的mkdtemp错误. mkdtemp 返回的路径名称为str,而不是上下文管理器.

You're using mkdtemp incorrectly. mkdtemp returns the path name as str, not a context manager.

如果要使用上下文管理器来管理临时目录,则需要使用

If you want a context manager for managing a temporary directory, you need to use TemporaryDirectory, which is available from Python 3.2 and above.

这篇关于Python:为什么我收到AttributeError:__enter__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆