RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 25111 | Replies: 11
Reply
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

os.popen

  #1  
Mar 9th, 2006
i don't understand the use of the os.popen function, i've used it to read things, for example:

>>> a = os.popen("netstat")
>>> for line in a.readlines():
	print line

	


Active Connections

>>>

i understand that, but there is also a buffersize and a write ability, but i've played about with it and i keep getting error's and stuff, i'm wondering if someone happens to know anything about the writing ability, i was thinking it would be cool if i could open some kind of pipe and write to it.

thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 2,541
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: os.popen

  #2  
Mar 14th, 2006
I looked around in code I had written and could only find these two examples ...
[php]# pipe calendar.prmonth(2005, 6) to a string
# save the short code within the triple quotes as cal_mar2006.py:
"""
import calendar
calendar.prmonth(2006, 3)
"""

import os

# give full path of .py file so python.exe can find it
p = os.popen("python D:/Python24/Atest/cal_mar2006.py")
# pipe the output to string str1
str1 = p.read()
print str1
[/php]
... and this ...
[php]# play a sound file, linux or windows choice:
import sys

# pick a soundfile you have ...
soundFile = "D:/Python24/Atest/Sound/game_over.wav"

if sys.platform[:5] == "linux":
winflag = 0
import os
os.popen2("aplay -q " + soundFile)
else:
winflag = 1
import winsound
winsound.PlaySound(soundFile, winsound.SND_FILENAME)
[/php]
os.popen() behaves similar to a file open(), the default is "r" for read, so for write to have to addd a "w". You can specify the buffer size in bytes or use the default of zero.
May 'the Google' be with you!
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: os.popen

  #3  
Mar 14th, 2006
yea i kinda got that it's the write bit i'm interested in, and i havn't managed to actually write anything without getting an error, lol i'll play with it
Reply With Quote  
Join Date: Oct 2004
Posts: 2,541
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: os.popen

  #4  
Mar 15th, 2006
What is the command/program you want to write to?
You could use something like ...
import os 
os.popen('program').write('whatever') 
May 'the Google' be with you!
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: os.popen

  #5  
Mar 15th, 2006
i was thinking msdos, as in i do summit simple to test like:
popen('start command.com').write('echo test')

something similiar to that?
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: os.popen

  #6  
Mar 16th, 2006
What little I know, your Python code looks correct. But, why the "start" part of command.com?

I know you can't really spawn new programs in DOS, but other than that it looks ok. It looks like you just use the write() method to provide input to whatever program, and that program has to accept input from stdin. I would imagine that's where your errors are coming from-- not from Python, but from the actual program that you're using os.popen() on.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: os.popen

  #7  
Mar 16th, 2006
yea i know, i could have just done 'start' or 'cmd' or something like that but it's the write bit where i get problems, all other cases i get an error EXCEPT for if i use
a = os.popen("cmd","w")
a.write("bla")
that gives me no error, but i also see f*all

and yea i can imagine it is from the program.
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: os.popen

  #8  
Mar 17th, 2006
What exactly is the program you're running, and what is the error you're getting?

If you can give the exact statements you're using, maybe we could provide some more help.

If creating an object like you demonstrated causes no issue, why not just run with it that way?
Last edited by alc6379 : Mar 17th, 2006 at 4:13 am. Reason: speeling
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: os.popen

  #9  
Mar 17th, 2006
i was just messing about, not really making anything, i have this little script i run at school to tell me when the teacher turns the monitoring software on, it basically does a netstat and checks for the port, but i have to keep opening a netstat pipe, i just thought it would maybe be faster if i just opened a dos shell through python and kept sending the same command. nothing really important i was just intreeged about the write part but i could never get it working.
>>> import os
>>> a = os.popen("cmd","w")
>>> a.write('echo test')

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in -toplevel-
    a.write('echo test')
IOError: [Errno 22] Invalid argument
>>> 

IOError: [Errno 22] Invalid argument is the error i tend to get all the time

cheers for the help
Reply With Quote  
Join Date: Oct 2004
Posts: 2,541
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: os.popen

  #10  
Mar 17th, 2006
Try to run this ...
import os
a = os.popen("cmd","w")
print a  # test
a.write('echo test')
Also what happens if you give it the full path of cmd.exe, or replace "cmd" with a program that does not exist like "cmdxxx"?
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:53 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC