read() or readline() clobbers my variable

W

washu

Hi,

This may be a simple problem but I can't seem to find a way around it.
I've pasted my code snippet below:

import os

input = os.popen("echo hello")
input.readline()
input.readline()

The first input.readline() gives the output. The second readline() gives
an empty line. It almost seems like input acts as a buffer which gets
emptied when you read it.

I even tried setting it equal to another variable (temp = input) but it
seems like temp receives a pointer that just points to input.
temp.readline() produces the same results.

I'd like to basically freeze the contents of input for further
manipulation or comparison. Any ideas?
 
V

vincent wehren

washu said:
Hi,

This may be a simple problem but I can't seem to find a way around it.
I've pasted my code snippet below:

import os

input = os.popen("echo hello")
input.readline()
input.readline()

The first input.readline() gives the output.

You just read one line from a file open for reading.
Then you try to read the next line, and there is no next line.
I even tried setting it equal to another variable (temp = input) but it
seems like temp receives a pointer that just points to input.
temp.readline() produces the same results.

I'd like to basically freeze the contents of input for further
manipulation or comparison. Any ideas?

Exactly: the *contents*.
 
P

Peter Otten

washu said:
This may be a simple problem but I can't seem to find a way around it.
I've pasted my code snippet below:

import os

input = os.popen("echo hello")
input.readline()
input.readline()

The first input.readline() gives the output. The second readline() gives
an empty line. It almost seems like input acts as a buffer which gets
emptied when you read it.

I even tried setting it equal to another variable (temp = input) but it
seems like temp receives a pointer that just points to input.
temp.readline() produces the same results.

I'd like to basically freeze the contents of input for further
manipulation or comparison. Any ideas?

In Python 2.4 you have another option in addition to Vincent's suggestion:
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

tee() creates two new iterators out of one original (which shouldn't be used
after the tee call). The nice thing about the next() method as opposed to
readline() is that it is called implicitely in a for loop.
....
hello
....
hello

Of course once you have consumed both a and b, the information from the
os.popen() call is lost just like in your readline() example -- unless you
put statements to store at least those lines you are interested in into the
for-loop(s).

Peter
 
G

G. S. Hayes

washu said:
Hi,

This may be a simple problem but I can't seem to find a way around it.
I've pasted my code snippet below:

import os

input = os.popen("echo hello")
input.readline()
input.readline() [SNIP]
I'd like to basically freeze the contents of input for further
manipulation or comparison. Any ideas?

Save it in a variable. readline is a function which reads the next
line from the open file. Consider the following examples:
hi
there
a=os.popen('echo "hi\nthere"')
print a.readlines() ['hi\n', 'there\n']
print a.readlines() []
a=os.popen('echo "hi\nthere"')
lines=a.readlines()
print lines ['hi\n', 'there\n']
print lines ['hi\n', 'there\n']
a=os.popen('echo "hi\nthere"')
line1=a.readline()
line2=a.readline()
print line1
hi
there
hi
there
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top