如何从“a +”中打开的文件中读取模式?

编程入门 行业动态 更新时间:2024-10-26 18:24:50
本文介绍了如何从“a +”中打开的文件中读取模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据定义,a +模式会打开文件以进行追加和阅读。附加作品,但阅读方法是什么?我做了一些搜索,但在任何地方都找不到它。

By definition, "a+" mode opens the file for both appending and reading. Appending works, but what is the method for reading? I did some searches, but couldn't find it clarified anywhere.

f=open("myfile.txt","a+") print (f.read())

试过这个,它打印为空白。

Tried this, it prints blank.

推荐答案

使用 f.seek()将文件偏移量设置为文件的开头。

Use f.seek() to set the file offset to the beginning of the file.

注意:在Python 2.7之前,有一个错误会导致某些操作系统没有文件位置总是指向文件结尾文件。这可能会导致某些用户使用您的原始代码。例如,在CentOS 6上,您的代码可以按照您的意愿工作,但不应该如此。

Note: Before Python 2.7, there was a bug that would cause some operating systems to not have the file position always point to the end of the file. This could cause some users to have your original code work. For example, on CentOS 6 your code would have worked as you wanted, but not as it should.

f = open("myfile.txt","a+") f.seek(0) print f.read()

更多推荐

如何从“a +”中打开的文件中读取模式?

本文发布于:2023-10-08 13:57:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!