Python 文件打开语法

标签 python python-2.7 pycharm

在Python 2.7中,这两个语句有什么区别:

f = open("file_name", "r")
f = open("file_name").read()

我知道两者都在打开文件,但是首先以读取模式打开文件,而后者打开文件然后读取它?

最佳答案

第一个将返回一个处于读取模式的打开文件对象。

第二个将返回读取模式下打开的文件对象的内容

关于Python 文件打开语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45444408/

相关文章:

python - session 未创建 : This version of ChromeDriver only supports Chrome version 88

Python keras - 模型层出现错误

python - 整数在SQlite中记录良好,但在MySQL中记录为0

python - 如何使用 Python 保存 "complete webpage"而不仅仅是基本的 html

html - 如何在 GoLand 或 PyCharm IDE 中打开与 html 文件相同的 .tmpl 文件?

python - 如何使用 python "windows search"模块执行带双引号的命令(net start 'os')?

python crontab 和路径

Python OpenCV 视频格式浏览器播放

python - "Ambiguous Class Definition"类名 "I"(Python 中的 Pep8)

python - pycharm 无法完成 Docker 的远程解释器设置