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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
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
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
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
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
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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417