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
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:
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'
Store the contents of the file in a variable, thus caching it in memory, then use that instead of re-reading the file.
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()
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.
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)