Attempting to read open file a second time gets no data

前端 未结 4 1794
情话喂你
情话喂你 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:24

    Once you read a complete file into memory, reading some more from that file will result in an empty string being returned:

    >>> example = open('foobar.txt')
    >>> example.read()
    'Foo Bar\n'
    >>> example.read()
    ''
    

    In other words, you have reached the end of the file. You have three alternatives here:

    1. Re-open the file for each complete read.
    2. Use .seek() to go to the start of the file again:

      >>> example = open('foobar.txt')
      >>> example.read()
      'Foo Bar\n'
      >>> example.seek(0)
      >>> example.read()
      'Foo Bar\n'
      
    3. Store the contents of the file in a variable, thus caching it in memory, then use that instead of re-reading the file.

    0 讨论(0)
  • 2020-12-02 01:31

    File objects are meant to be read once. Once fin has been read(), you can no longer read anything else from the file since it has already reached EOF.

    To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").

    http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects

    If you really need reentrant access to your file use:

    def lst():
      fin.seek(0)
      return fin.readlines()
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 01:39

    Once you've called read() once fin has been read.

    If you want to read() it again (having discarded the contents on the first attempt), you'll need to re-open() or seek() the start of the file.

    I suggest you do this in lst(), or, better still, store the contents returned by lst()

    Aside from that...

    fin.write(str(len(lst()) + in)
    

    It's not clear what you're trying to do here, but you'd be better off using a stored return value from lst() and not using in as a variable name, since it's a reserved keyword.

    Something like:

    lines = lst()
    # [...]
    number_of_lines = len(lines)
    fin.write(str(number_of_lines) + some_other_str)
    
    0 讨论(0)
提交回复
热议问题