- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我希望能够打开一个文件,在末尾附加一些文本,然后只读取第一行。我确切地知道文件的第一行有多长,而且文件足够大,我不想一次将它读入内存。我试过使用:
with open('./output files/log.txt', 'a+') as f:
f.write('This is example text')
content = f.readline()
print(content)
但是打印语句是空白的。当我尝试使用 open('./output files/log.txt')
或 open('./output files/log.txt', 'r+')
时open('./output files/log.txt', 'a+')
这行得通,所以我知道它与 'a+
参数有关。我的问题是我必须附加到文件。如何在不使用类似
with open('./output files/log.txt', 'a+') as f_1:
f.write('This is example text')
with open('./output files/log.txt') as f_2:
content = f_2.readline()
print(content)
最佳答案
当您打开带有附加标志 a
的文件时,它会将文件描述符的指针移动到文件末尾,以便 write
调用将添加到文件的结尾。
readline()
函数从文件的当前指针开始读取,直到它读取的下一个'\n'
字符。所以当你用 append 打开一个文件,然后调用 readline
时,它会尝试从文件末尾开始读取一行。这就是为什么您的 print
调用变成空白的原因。
您可以使用 tell()
函数查看 file
对象当前指向的位置,从而了解实际情况。
要阅读第一行,您必须确保文件的指针回到文件的开头,您可以使用 seek
来做到这一点。功能。 寻找
takes two arguments : offset
和 from_what
。如果省略第二个参数,则 offset
从文件的开头获取。所以要跳转到文件的开头,请执行:seek(0)
。
如果你想跳回文件末尾,你可以包含from_what
选项。 from_what=2
表示从文件末尾开始偏移。所以要跳到最后:seek(0, 2)
。
以附加模式打开文件指针时的演示:
使用如下所示的文本文件的示例:
the first line of the file
and the last line
代码:
with open('example.txt', 'a+') as fd:
print fd.tell() # at end of file
fd.write('example line\n')
print fd.tell() # at new end of the file after writing
# jump to the beginning of the file:
fd.seek(0)
print fd.readline()
# jump back to the end of the file
fd.seek(0, 2)
fd.write('went back to the end')
控制台输出:
45
57
the first line of the file
example.txt
的新内容:
the first line of the file
and the last line
example line
went back to the end
编辑:添加跳回文件末尾
关于python - 你能用 open(fname, 'a+' ) 从文件中读取第一行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514177/
为了设置场景,我使用了 jQuery。我在另一个答案中读到“某些库 (jQuery) 使用 $ 作为主要基础对象(或函数)”。 这很好,但我感到困惑的是为什么我可以拥有以下内容并且它有效: $(doc
下面是我的 php 代码片段,请问当我搜索 john doe 时如何使我的搜索返回结果我在我的 php 表单中使用单个搜索框,fname 和 lname 保存在我数据库的不同列中。我只在只搜索 fna
我必须检查多个文件的存在和可读性。哪种方法最有效? list_of_files = [fname1,fname2,fname3] for fname in list_of_files: try
我的程序中有一个简单的查询字符串: ?username=someone@email.com&fname= 我想出了一个正则表达式,它可以选择除我想要的数据之外的所有内容: [^a-zA-Z0-9.@]
这个问题在这里已经有了答案: C# 7.0 ValueTuples vs Anonymous Types (2 个答案) 关闭去年。 这两个查询有什么区别? var query = from e i
我正在尝试将 fname lname 和电话号码 (number) 的用户条目读入私有(private)类中相应命名的函数中。因为我不知道如何直接从输入流 (cin >> get_fname();)
程序是读回多个bin文件在“主”程序结束时发生上述错误。我哪里代码错了?谢谢你的帮助 char* read_back(const char* filename) { FILE*
public class Registration { public static void main(String[] args) { final String MY
学生输入他的凭据: Enter Your First Name: Enter Your Last Name: Enter Your Student Number: 我有一张学生记录表如
表格: Full Name : Next AJAX: $('#existName').c
我有一个很大的 .cbr 文件夹,我正在按问题编号重命名它们以正确排序它们。我需要在 ren 行中包含什么才能让每个文件通过 Windows 命令提示符增加文件名中的数字?我会经常这样做,所以我会把它
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
这是 HTML 表单。单击“编辑”按钮时,我需要将条目放回表格。它指向脚本函数 selectEdit()
给定一个像“james bond”或“roger r burns”这样的字符串,我想创建一个接受输入并返回名字和姓氏的方法: 对于上述情况: Input: James Bond Returns:
谁能告诉我怎么做? 问题是 @fname 包含引号,因此连接 %source% 和 @fname 会出错... forfiles /P "%source%" /M %file%.* /D -1 /C
我想为 NGINX rtmp 模块使用 HLS_Variant 功能。 但是,如果我按照文档中的示例进行操作,我将无法正常工作。 我有以下测试: application Test {
不是 Perl 大师,使用古老的脚本,遇到了一个我不认识的结构,它产生了我意想不到的结果。想知道这是标准语言还是某种 PM 定制: open FILE1, ">./$disk_file" or die
var eachMapping = [{"fname":"John"},{"fname":"John"}]; var count = 0; for(var i = 0; i < eachMap
标题几乎说明了一切。 我正在制作一个表格,该表格将使用 div class = "well"在引号中返回您的名字和姓氏以及城市。我现在被困了几个小时,试图找出我做错了什么。 /* get info
我希望能够打开一个文件,在末尾附加一些文本,然后只读取第一行。我确切地知道文件的第一行有多长,而且文件足够大,我不想一次将它读入内存。我试过使用: with open('./output files/
我是一名优秀的程序员,十分优秀!