945,066 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 89580
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 9th, 2006
0

os.popen

Expand Post »
i don't understand the use of the os.popen function, i've used it to read things, for example:

Python Syntax (Toggle Plain Text)
  1. >>> a = os.popen("netstat")
  2. >>> for line in a.readlines():
  3. print line
  4.  
  5.  
  6.  
  7.  
  8. Active Connections
  9.  
  10. >>>

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
Similar Threads
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Mar 14th, 2006
0

Re: os.popen

I looked around in code I had written and could only find these two examples ...
python Syntax (Toggle Plain Text)
  1. # pipe calendar.prmonth(2005, 6) to a string
  2. # save the short code within the triple quotes as cal_mar2006.py:
  3. """
  4. import calendar
  5. calendar.prmonth(2006, 3)
  6. """
  7.  
  8. import os
  9.  
  10. # give full path of .py file so python.exe can find it
  11. p = os.popen("python D:/Python24/Atest/cal_mar2006.py")
  12. # pipe the output to string str1
  13. str1 = p.read()
  14. print str1
  15. [/php]
  16. ... and this ...
  17. [php]# play a sound file, linux or windows choice:
  18. import sys
  19.  
  20. # pick a soundfile you have ...
  21. soundFile = "D:/Python24/Atest/Sound/game_over.wav"
  22.  
  23. if sys.platform[:5] == "linux":
  24. winflag = 0
  25. import os
  26. os.popen2("aplay -q " + soundFile)
  27. else:
  28. winflag = 1
  29. import winsound
  30. 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.
Last edited by vegaseat; Oct 1st, 2009 at 4:40 pm. Reason: fixed code tags
Moderator
Reputation Points: 1344
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 14th, 2006
0

Re: os.popen

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
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Mar 15th, 2006
0

Re: os.popen

What is the command/program you want to write to?
You could use something like ...
Python Syntax (Toggle Plain Text)
  1. import os
  2. os.popen('program').write('whatever')
Moderator
Reputation Points: 1344
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 15th, 2006
0

Re: os.popen

i was thinking msdos, as in i do summit simple to test like:
Python Syntax (Toggle Plain Text)
  1. popen('start command.com').write('echo test')

something similiar to that?
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Mar 16th, 2006
0

Re: os.popen

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Mar 16th, 2006
0

Re: os.popen

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
Python Syntax (Toggle Plain Text)
  1. a = os.popen("cmd","w")
  2. a.write("bla")
that gives me no error, but i also see f*all

and yea i can imagine it is from the program.
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Mar 17th, 2006
0

Re: os.popen

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
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Mar 17th, 2006
0

Re: os.popen

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.
Python Syntax (Toggle Plain Text)
  1. >>> import os
  2. >>> a = os.popen("cmd","w")
  3. >>> a.write('echo test')
  4.  
  5. Traceback (most recent call last):
  6. File "<pyshell#2>", line 1, in -toplevel-
  7. a.write('echo test')
  8. IOError: [Errno 22] Invalid argument
  9. >>>

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

cheers for the help
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Mar 17th, 2006
0

Re: os.popen

Try to run this ...
Python Syntax (Toggle Plain Text)
  1. import os
  2. a = os.popen("cmd","w")
  3. print a # test
  4. 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"?
Moderator
Reputation Points: 1344
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Use Calendar in GUI
Next Thread in Python Forum Timeline: raw_input exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC