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