【发布时间】:2018-02-17 03:20:28
【问题描述】:
是否可以同时读取和写入同一个文件?我有这段代码:
with open(total_f, 'w') as f:
s= len(resu)
#print s
f.write ("the total number is {}\n".format(s))
f.write('\n'.join(map(str, resu)))
with open(total_f ,'r') as f:
content = f.read()
count = content.count("FFFF")
print count
但我需要他们在一起,因为我想把count的结果写在total_f中。
我试着像这样把读/写放在一起:
with open(total_f, 'rw') as f:# i want to read and write at the same time
s= len(resu)
content = f.read()
count = content.count("FFFF")
#print s
f.write ("the total number is {}\n".format(s))
f.write ("the total number is {}\n".format(count))
f.write('\n'.join(map(str, resu)))
但它仍然无法正常工作,所以我不确定这是否正确。
【问题讨论】:
-
运行代码时会发生什么?
-
我有单独的结果,正在寻找一种方法将打开的 total_f 也作为读取,并将结果写入同一个文件
-
我不知道“我有单独的结果”是什么意思。
-
我现在将编辑问题,
标签: python file-writing