Attempting to read open file a second time gets no data

前端 未结 4 1810
情话喂你
情话喂你 2020-12-02 00:47
fin = open(\'/abc/xyz/test.txt\', \'a+\')

def lst():
  return fin.read().splitlines()

print lst()

def foo(in):
  print lst()
  fin.write(str(len(lst()) + in)
  fi         


        
4条回答
  •  温柔的废话
    2020-12-02 01:35

    File object can only read once which means if you'v called fin.read() before the later fin.read() will return nothing.

    fix this by call fin.seek(0) after call fin.read(), or read file to some buffer.

提交回复
热议问题