sys.stdin.read question

I

It's me

Why do I get an "AttributeError: read" message when I do:

import sys
r=sys.stdin.read()

??

I've tried:

r=sys.stdin.read(80)
r=sys.stdin.read(1)

same error message.

I couldn't find any reference to this function in my Python book (they have
the stdout but not in).

Some sample code I saw uses this function in the same manner I am and so I
am assuming this is the correct syntax?

Or is this a bug in Python 2.4?
 
G

Grant Edwards

Why do I get an "AttributeError: read" message when I do:

import sys
r=sys.stdin.read()

Dunno. Works fine for me under 2.3.4, and according to the
docs, should work under 2.4.

What do you get when you do this:

import sys
type(sys.stdin)
dir(sys.stdin)
Some sample code I saw uses this function in the same manner I
am and so I am assuming this is the correct syntax?

Should be.
Or is this a bug in Python 2.4?

That would be a little hard to believe.
 
I

It's me

Grant Edwards said:
Dunno. Works fine for me under 2.3.4, and according to the
docs, should work under 2.4.

What do you get when you do this:

import sys

Done that.
type(sys.stdin)

I get:

said:
dir(sys.stdin)

I get:

['_RPCProxy__attributes', '_RPCProxy__getattributes',
'_RPCProxy__getmethods', '_RPCProxy__methods', '__doc__', '__getattr__',
'__init__', '__module__', 'encoding', 'oid', 'sockio']
Should be.


That would be a little hard to believe.

Well, here's a copy from the Python Shell output:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in -toplevel-
print sys.stdin.read(5)
AttributeError: read
????????
 
G

Grant Edwards

Dunno. Works fine for me under 2.3.4, and according to the
docs, should work under 2.4.

What do you get when you do this:

import sys

Done that.
type(sys.stdin)

I get:

dir(sys.stdin)

I get:

['_RPCProxy__attributes', '_RPCProxy__getattributes',
'_RPCProxy__getmethods', '_RPCProxy__methods', '__doc__', '__getattr__',
'__init__', '__module__', 'encoding', 'oid', 'sockio']

As somebody else already suggested, you must be running your
program inside some sort of IDE that replaces sys.stdin with
some other object that doesn't have a read() method. Try
running the program from a shell prompt.
 
G

Grant Edwards

It runs properly in a shell (bash), but on another matter:

'>>> r=sys.stdin.read(1)
g
'>>> r
'g'
'>>> r=sys.stdin.read(5)
1234567890
'>>> r
'\n1234'
'>>>

What exactly happened to my 1234567890? I understand that I am only
taking 5 characters, but where does the newline (\n) come from? Is that a
remnant from when I terminated the previous 'g' input?

Exactly.

The input stream consisted of 'g\n1234567890\n'
 
L

Lars Tengnagel

Hej Caleb and others

I've been strugling with the same problem where i try to use popen3 to run a
program. If I use a piped commandline
the program can read the file without problems but in the IDLE and with
popen it comes with an error.
I haven't been able to read the stdin either so the problem so far is
unsolved for my point.
But the newline command would explain my problems with the program.
Can it be a problem under windows since I'm using XP and the winpython

Hopefully Lars

Caleb Hattingh said:
It runs properly in a shell (bash), but on another matter:

'>>> r=sys.stdin.read(1)
g
'>>> r
'g'
'>>> r=sys.stdin.read(5)
1234567890
'>>> r
'\n1234'
'>>>

What exactly happened to my 1234567890? I understand that I am only
taking 5 characters, but where does the newline (\n) come from? Is that a
remnant from when I terminated the previous 'g' input?

Thanks
Caleb
 
I

It's me

Yes, if I run the script from the command prompt, it works. I was running
it inside the Python IDE.

Thanks,

--
It's me


Grant Edwards said:
Dunno. Works fine for me under 2.3.4, and according to the
docs, should work under 2.4.

What do you get when you do this:

import sys

Done that.
type(sys.stdin)

I get:

dir(sys.stdin)

I get:

['_RPCProxy__attributes', '_RPCProxy__getattributes',
'_RPCProxy__getmethods', '_RPCProxy__methods', '__doc__', '__getattr__',
'__init__', '__module__', 'encoding', 'oid', 'sockio']

As somebody else already suggested, you must be running your
program inside some sort of IDE that replaces sys.stdin with
some other object that doesn't have a read() method. Try
running the program from a shell prompt.
SERBO-CROATIAN
 
C

Caleb Hattingh

Hi

You are probably typing this within IDLE. Try it after starting python in
a shell like DOS or Bash. Should work then (works for me, and I also get
the AttributeError in IDLE.

Thanks
Caleb
 
C

Caleb Hattingh

It runs properly in a shell (bash), but on another matter:

'>>> r=sys.stdin.read(1)
g
'>>> r
'g'
'>>> r=sys.stdin.read(5)
1234567890
'>>> r
'\n1234'
'>>>

What exactly happened to my 1234567890? I understand that I am only
taking 5 characters, but where does the newline (\n) come from? Is that a
remnant from when I terminated the previous 'g' input?

Thanks
Caleb


Hi

You are probably typing this within IDLE. Try it after starting python
in a shell like DOS or Bash. Should work then (works for me, and I also
get the AttributeError in IDLE.

Thanks
Caleb
 
C

Caleb Hattingh

I don't have much experience with popen3. I do know that IDLE
(interactive interpreter) does something to sys.stdin, and that is
probably the problem you are seeing. Try your commands through the python
interactive interpreter started from a shell (DOS or Bash), see if it
still happens.

thx
Caleb
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top