Python - 何时使用文件打开 [英] Python - When to use file vs open

查看:104
本文介绍了Python - 何时使用文件打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中的文件和打开有什么区别?什么时候应该使用哪一个? (说我在2.5)

解决方案

你应该始终使用 open()



由于文档指出:


打开文件时,最好使用
open()而不是直接调用这个
构造函数。文件更多是
适合进行类型测试(例如
写入isinstance(f,file))。


另外, file() 已被删除


What's the difference between file and open in Python? When should I use which one? (Say I'm in 2.5)

解决方案

You should always use open().

As the documentation states:

When opening a file, it's preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing "isinstance(f, file)").

Also, file() has been removed since Python 3.0.

这篇关于Python - 何时使用文件打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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