Please support our Python advertiser: Programming Forums
Views: 25111 | Replies: 11
![]() |
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
i don't understand the use of the os.popen function, i've used it to read things, for example:
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
>>> 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
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.
[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!
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
What is the command/program you want to write to?
You could use something like ...
You could use something like ...
import os
os.popen('program').write('whatever')
May 'the Google' be with you!
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
i was thinking msdos, as in i do summit simple to test like:
something similiar to that?
popen('start command.com').write('echo test')something similiar to that?
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation:
Rep Power: 11
Solved Threads: 102
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.
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
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
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
that gives me no error, but i also see f*all
and yea i can imagine it is from the program.
a = os.popen("cmd","w")
a.write("bla")and yea i can imagine it is from the program.
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation:
Rep Power: 11
Solved Threads: 102
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?
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
•
•
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation:
Rep Power: 4
Solved Threads: 9
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.
IOError: [Errno 22] Invalid argument is the error i tend to get all the time
cheers for the help
>>> 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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode