read() or readline() clobbers my variable
vincent at visualtrans.de
Fri Jul 30 10:37:12 CEST 2004
> 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")
> 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*.
>>> import os
>>> infile = os.popen("echo hello world")
<open file 'echo hello world', mode 'r' at 0x00A8E560>
>>> contents = infile.read()
>>> # now do to contents whatever you want
More information about the Python-list