Why is reading from a file not working after using a function?

2300 views python
6

My code is supposed to find the occurrence of words in a text. For some reason, after using this function, the reading method from this specific textwon't work(empty lists for .readlines() method and nothing for .read()). The code is:

def counter(new):
    words = dict()
    lines=new.readlines()
    for line in lines:
        text=line.split()
        for word in text:
            if word not in words:
                words[word]=1
            else: words[word]+=1
    return [tuple(x) for x in words.items()]

I haven't found any mistake that can cause this.

answered question

"won't work" is not a very descriptive problem description. Please clarify. Provide a Minimal, Complete, and Verifiable example that others can see.

If you expect to read from new after calling that function, you're out of luck, since you've already read the whole thing. If you want to read it again for some reason, you'll need to "rewind" it with the .seek method.

Please show the code which does not behave as expected (and please state what you expected and how it behaved). This code does not do anything until it is called, and then what it does depends on what the argument is. What does not work after that is absolutely impossible to know without more info.

@PM2Ring or perhaps more sanely, simply open a new file-object

@juanpa.arrivillaga .seek is much cheaper than closing & reopening the file.

@PM2Ring sure, but manually seeking around files, in my experience, can be tricky if you aren't careful. Unless I'm trying to optimize something, I will pretty much always just open a new file-object.

2 Answers

7

I am assuming new is the result of calling open(..), which should be a file handle that you are passing into the function. Calling readlines() is exhausting it, so trying to read from the file using the same handle won't work. Either open a new handle, or you can call new.seek(0) (better option than opening a completely new handle - although there are some cases where the same handle will be returned again, but lets not dive too deep into that here) to move the index to the beginning of the file.

posted this
10

I want to say the issue is how you were passing the file into your function, make sure your file is set to a variable with filename = open('sample.txt', 'r')

Using sample.txt:

one 
two
three
four
five
hey
hi
bye
this
that
bye
one
two
hat
one
one

With code:

def counter(new):
    words = dict()
    lines=new.readlines()
    for line in lines:
        text=line.split()
        for word in text:
            if word not in words:
                words[word]=1
            else: words[word]+=1
    return [tuple(x) for x in words.items()]

filename = open('sample.txt', 'r')
a = counter(filename)
print(a)

Output:

(xenial)vash@localhost:~/python/stack_overflow$ python3.7 else.py
[('one', 4), ('two', 2), ('three', 1), ('four', 1), ('five', 1), 
('hey', 1), ('hi', 1), ('bye', 2), ('this', 1), ('that', 1), ('hat', 1)]

posted this

Have an answer?

JD

Please login first before posting an answer.