473,795 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1777
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(o s.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(o s.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
3678
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()) != null) { LastLine = file.ReadLine(); }//while readline
5
1998
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 VS.Net 2005 to render the associated .resx and .vb file inoperative. detail... I converted an asp 1.1 vb.net app using the vs2005 conversion wizard and then retested the app after conversion.
3
2279
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
3738
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: class GeneInfo(): def __init__(self): #urllib.urlretrieve('ftp://ftp.ncbi.nih.gov/gene/DATA/ gene_info.gz',"/tmp/gene_info.gz")
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.