fd = file(filename, "r",encoding='utf-8')
上句运行出现错误:NameError: name 'file' is not defined
解决办法:file()改为open(),python 3.0 不再支持file
2to3 fixer ☐ six support ☐
In Python 2 there is a file type builtin. This is replaced with various file types in Python 3. You commonly see code in Python 2 that uses file(pathname)which will fail in Python 3.
Replace this usage with open(pathname).If you need to test for types you can in Python 3 check for io.IOBase instead of file.
IOBase是一个上下文管理器,所以也支持with语句。在这个例子中,文件在with语句块执行完成后被关闭,即使错误发生也会关闭文件。
with open(', 'w') as file: file.write('spam and eggs')
转载于:.html
本文发布于:2024-01-29 03:04:08,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170646865112241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |