user808545
user808545

Reputation: 1631

error: integer required readlines()

I'm trying to open a CSV document using readlines() in Python.

The document contains a list of words, but I'm getting the error that an integer is required. Here's what I wrote and the error message I received:

>>> f = open('mike_only_genes.csv')
>>> a = f.readlines('mike_only_genes.csv')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

Upvotes: 2

Views: 1320

Answers (3)

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29121

Read more about readlines method: if sizehint parameter is specified than 'sizehint bytes (possibly after rounding up to an internal buffer size) are read', but you have specified a string that is why it throwing an exception about integer value:

f = open('mike_only_genes.csv') 
a = f.readlines() 

OR use:

with open('mike_only_genes.csv') as f:
    for line in f:
        # do something

OR:

for line in open('mike_only_genes.csv'):
    # do something

Upvotes: 0

utdemir
utdemir

Reputation: 27256

Because readlines can be executed alone or with an integer, if you want to read all lines, just use readlines().

with open('mike_only_genes.csv') as f:
    lines = f.readlines()

Upvotes: 1

Corey Goldberg
Corey Goldberg

Reputation: 60644

you are calling it wrong:

f.readlines('mike_only_genes.csv')

should be:

f.readlines()

Upvotes: 3

Related Questions