Jython

G

gregturn

Hi

Why this doesn't work:

def go():
for line in open("bobo.txt", "r"):
print line

go()

python FileReader.py: everything ok
jython FileReader.py:

Traceback (innermost last):
File "FileReader.py", line 6
File "FileReader.py", line 3
AttributeError: __getitem__

Files aren't lists and thus don't have the functions for iteration.

Try:

def go():
for line in open("bobo.txt", "r").readlines():
print line

go()
 
B

Boris Ozegovic

Hi

Why this doesn't work:

def go():
for line in open("bobo.txt", "r"):
print line

go()

python FileReader.py: everything ok
jython FileReader.py:

Traceback (innermost last):
File "FileReader.py", line 6
File "FileReader.py", line 3
AttributeError: __getitem__
 
B

Boris Ozegovic

Files aren't lists and thus don't have the functions for iteration.

They do have iterator:

C:\Documents and Settings\Silovana Vjeverica\Desktop>python FileReader.py
'import site' failed; use -v for traceback
boris ozegovic
vedran ozegovic
 
B

Boris Ozegovic

Boris said:
They do have iterator:

C:\Documents and Settings\Silovana Vjeverica\Desktop>python FileReader.py
'import site' failed; use -v for traceback
boris ozegovic
vedran ozegovic

But apparently not in Jython... Only Python.

Tnx, anyway
 
B

Boris Ozegovic

Files aren't lists and thus don't have the functions for iteration.

Try:

def go():
for line in open("bobo.txt", "r").readlines():
print line

go()

For example, you can do even this:

import sys

for line in sys.stdin:
print line,


python FileReader.py < bobo.tx

'import site' failed; use -v for traceback
boris ozegovic
vedran ozegovic
 
P

Peter Otten

Boris said:
But apparently not in Jython... Only Python.

Iterable files were introduced in Python 2.2. Jython implements Python2.1,
it seems.

Peter
 
B

bearophileHUGS

(e-mail address removed):
Files aren't lists and thus don't have the functions for iteration.
Try:
def go():
for line in open("bobo.txt", "r").readlines():
print line
go()

CPython 2.1 has xreadlines, maybe Jython has it too.

Bye,
bearophile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top