Python初试云雨情

阅读: 评论:0

Python初试云雨情

Python初试云雨情

fd = file(filename, "r",encoding='utf-8')  
上句运行出现错误:NameError: name 'file' is not defined

解决办法:file()改为open(),python 3.0 不再支持file

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小时内删除。

标签:云雨   初试   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23