单选题
适中
#16237
执行以下代码后,file.txt 文件的内容是?( ) with open('file.txt', 'w') as f: f.write('First line…
执行以下代码后,file.txt 文件的内容是?( )
with open('file.txt', 'w') as f:
f.write('First line\n')
with open('file.txt', 'a') as f:
f.write('Second line\n')
with open('file.txt', 'r+') as f:
f.seek(0)
f.write('Replaced first line\n')
答案与解析
正确答案:B
解析
首先,以 'w' 模式写入 "First line\n";接着,以 'a' 模式追加 "Second line\n";最后,以 'r+' 模式打开文件,使用 seek(0) 将文件指针移动到文件开头,然后写入 "Replaced first line\n",会覆盖原有的第一行内容,所以最终文件内容是 "Replaced first line\nSecond line\n"。