954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

os.popen

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

a1eio
Junior Poster
141 posts since Aug 2005
Reputation Points: 26
Solved Threads: 25
 

I looked around in code I had written and could only find these two examples ...

# 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)

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

a1eio
Junior Poster
141 posts since Aug 2005
Reputation Points: 26
Solved Threads: 25
 

What is the command/program you want to write to?
You could use something like ...

import os 
os.popen('program').write('whatever')
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

i was thinking msdos, as in i do summit simple to test like:

popen('start command.com').write('echo test')


something similiar to that?

a1eio
Junior Poster
141 posts since Aug 2005
Reputation Points: 26
Solved Threads: 25
 

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.

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

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.

a1eio
Junior Poster
141 posts since Aug 2005
Reputation Points: 26
Solved Threads: 25
 

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?

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

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 :)

a1eio
Junior Poster
141 posts since Aug 2005
Reputation Points: 26
Solved Threads: 25
 

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"?

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

perhaps you need to use " " instead of ' ' quotes?

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 
perhaps you need to use " " instead of ' ' quotes?


I think Python accepts either " or ' as long as they match.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You