Python 调用f.write()后文件仍然为空
这个程序(用Python编写)应该只显示写入到文件的前两行文本 特定文件。但是,当我运行它时,虽然它没有错误,但它不会在IDE中显示任何输出 也不在文件本身上Python 调用f.write()后文件仍然为空,python,output,Python,Output,这个程序(用Python编写)应该只显示写入到文件的前两行文本 特定文件。但是,当我运行它时,虽然它没有错误,但它不会在IDE中显示任何输出 也不在文件本身上 def file_read_from_head(fname, nlines): from itertools import islice with open(fname) as f: for line in islice(f, nlines): print(line) f = o
def file_read_from_head(fname, nlines):
from itertools import islice
with open(fname) as f:
for line in islice(f, nlines):
print(line)
f = open('test.txt', 'w')
f.write = ("Hello welcome to Python \n"
"THis is the second line \n"
"This is the third line \n")
print(file_read_from_head('test.txt', 2))
请注意:
f.write()
,而不是
将输出分配给名为f.write的变量
f.write()
后,还需要关闭该文件。您可以使用f.close()
实现这一点然而,使用a是更好的做法(请参见此答案下面的注释)。使用上下文管理器可以更容易地避免错误(例如忘记关闭文件…)。实际上,您已经在文件\u read\u from\u head()
函数中使用了上下文管理器file\u read\u from\u head()
调用print(line)
,因此不需要print(file\u read\u from\u head())
from itertools import islice
def file_read_from_head(fname, nlines):
with open(fname) as f:
for line in islice(f, nlines):
print(line)
# Context managers make it easier to avoid forgetting to close files
with open('test.txt', 'w') as f:
f.write("Hello welcome to Python \n"
"This is the second line \n"
"This is the third line \n")
file_read_from_head('test.txt', 2)
请注意:
f.write()
,而不是
将输出分配给名为f.write的变量
f.write()
后,还需要关闭该文件。您可以使用f.close()
实现这一点然而,使用a是更好的做法(请参见此答案下面的注释)。使用上下文管理器可以更容易地避免错误(例如忘记关闭文件…)。实际上,您已经在文件\u read\u from\u head()
函数中使用了上下文管理器file\u read\u from\u head()
调用print(line)
,因此不需要print(file\u read\u from\u head())
from itertools import islice
def file_read_from_head(fname, nlines):
with open(fname) as f:
for line in islice(f, nlines):
print(line)
# Context managers make it easier to avoid forgetting to close files
with open('test.txt', 'w') as f:
f.write("Hello welcome to Python \n"
"This is the second line \n"
"This is the third line \n")
file_read_from_head('test.txt', 2)
你做了一些错事。首先,f.write是一个函数,而不是一个变量。因此,将其替换为:
f.write("Hello welcome to Python \n"
"THis is the second line \n"
"This is the third line \n")
f.close()
with open(fname, 'r') as f:
在file_read_from_head函数中打开文件时,您没有提供读/写标志,因此请将其替换为:
f.write("Hello welcome to Python \n"
"THis is the second line \n"
"This is the third line \n")
f.close()
with open(fname, 'r') as f:
另外,您正在打印文件\u read\u from\u head的结果,该结果将为none,因此如果需要,您应该删除函数调用周围的print()。您做了一些错误的事情。首先,f.write是一个函数,而不是一个变量。因此,将其替换为:
f.write("Hello welcome to Python \n"
"THis is the second line \n"
"This is the third line \n")
f.close()
with open(fname, 'r') as f:
在file_read_from_head函数中打开文件时,您没有提供读/写标志,因此请将其替换为:
f.write("Hello welcome to Python \n"
"THis is the second line \n"
"This is the third line \n")
f.close()
with open(fname, 'r') as f:
另外,您正在打印文件从头读取的结果,该结果将是无的,因此如果您愿意,您应该删除函数调用周围的print()。即
=
不应出现在f.write=(…)
中。您想调用f.write
,而不是分配给它。你也应该在写完之后(在你尝试阅读之前)关闭f
。@khelwood说了什么。此外,您可能需要在读取文件之前关闭文件(f.close()
),因为文本首先写入缓冲区。关闭文件可确保缓冲区中的所有文本都写入磁盘上的实际文件。=
不应出现在f.write=(…)
中。您想调用f.write
,而不是分配给它。你也应该在写完之后(在你尝试阅读之前)关闭f
。@khelwood说了什么。此外,您可能需要在读取文件之前关闭文件(f.close()
),因为文本首先写入缓冲区。关闭文件可确保缓冲区中的所有文本都写入磁盘上的实际文件。与其建议关闭
文件,不如建议使用和
而不是像OP那样打开和关闭文件没有问题。但是我同意,使用上下文管理器要干净得多(我在回答中也暗示了这一点)。打开和关闭文件有一些问题,上面的问题中介绍了其中一个问题。。。。我想你在我写评论的时候把它编辑对了。我也希望代码能够匹配建议增益,这并没有错。是的,它更容易出错,而且不太像Python,我一直主张使用上下文管理器。但这不是“错”是的,我想这场争论会永远持续下去,因为这取决于你如何定义错误。。。处理文件的惯用方法和最佳方法是将与
一起使用。您的答案建议这样做,这是完美的,我只需更改代码示例,并在我的答案中完全避免使用open/close
,而不是建议close
文件,您真的应该建议使用with
而不是像OP那样打开和关闭文件没有错。但是我同意,使用上下文管理器要干净得多(我在回答中也暗示了这一点)。打开和关闭文件有一些问题,上面的问题中介绍了其中一个问题。。。。我想你在我写评论的时候把它编辑对了。我也希望代码能够匹配建议增益,这并没有错。是的,它更容易出错,而且不太像Python,我一直主张使用上下文管理器。但这不是“错”是的,我想这场争论会永远持续下去,因为这取决于你如何定义错误。。。处理文件的惯用方法和最佳方法是将与
一起使用。您的答案建议这样做,这是完美的,我也会更改代码示例,并在您的第二个建议中避免在我的答案中使用open/close