473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

read() or readline() clobbers my variable

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?
Jul 18 '05 #1
2 1762
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

Jul 18 '05 #2
washu wrote:
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:
import os, itertools
a, b = itertools.tee(os.popen("echo hello"))
a.next() 'hello\n' a.next() Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration b.next() 'hello\n' b.next() 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.
a, b = itertools.tee(os.popen("echo hello"))
for i in a: print i ....
hello
for i in b: print i

....
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kaiser | last post by:
Hello. I am trying to write a program that continues to read the last line of a txt file until a stop button is clicked. while (ContinueLoop == true ) { while ((LastLine = file.ReadLine()) !=...
5
by: GaryDean | last post by:
(my original post was inaccurate but this post accurately describes what I think is a very bad vs2005 bug) short description... Deleting a dataset and recreating it from the dataadapter causes...
3
by: ad | last post by:
I have a text file in the directory of my web application. How can I read this text file into a string vaiable?
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.