LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-01-2006, 10:49 PM   #1
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Rep: Reputation: 15
redirect stdout of os.system()?


How to redirect the stdout of "os.system(), os.execlp() or os.spawnlp() to a file or List(one line, one item)?

Code:
      1 #!/usr/bin/python
      2 # -*- coding: UTF-8 -*-
      3 
      4 import os
      5 import sys
      6 
      7 class stdout:
      8     def __init__(self):
      9         self.result = []
     10     def write(self, str):
     11         self.result = str.split('\n')
     12     def getlist(self):
     13         return self.result
     14 
     15 STDOUT = sys.stdout
     16 sys.stdout = open('tmp', 'w')
     17 os.system('ls -l')
     18 # os.spawnlp(os.P_WAIT, 'ls', '')
     19 tmp = sys.stdout
     20 sys.stdout = STDOUT
     21 # print tmp.getlist()
these 2 ways: use a file or a custom object, take no effect.

Thanks.
 
Old 03-02-2006, 01:03 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
It looks to me like you're trying to alter the current stdout in order to change a subprocess... your python script has its own stdout that is independent of the command you're running.

There are (at least) two ways to do what you want to do. One would be to run the command with shell redirection, e.g:
Code:
os.system( 'ls -l > stdout.log' )
and then read the file you created.

The other way is to use one of the popen commands. These return file handles for some combination of stdin, stdout and stderr (depending on whether you use popen, popen2, popen3, or popen4). These functions are in the 'os' module, check out the docs for details.

Let me know if I have misunderstood...
 
Old 03-02-2006, 01:27 AM   #3
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
Yes I have tried os.popen() and it works.

L = [ S.strip('\n') for S in os.popen('ls').readlines() ]
 
Old 03-02-2006, 01:54 AM   #4
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
And now I need to get some files from remote with ssh, with the paramiko package, the process hung up sometimes. Now I want to try the pexpect to load ssh/scp, and get the output to a list just like os.popen(). How to?

Thanks
 
Old 03-02-2006, 08:25 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I'm not very familiar with pexpect, but it looks like you can assign a file handle to child.logfile, where child is the pexpect object you create. You could try combining that with the read-end of the pipe() function to avoid creating a file.
 
Old 03-02-2006, 07:57 PM   #6
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
Yes I have looked up the doc of python, and I think I can use spawn.logfile. Now I tried this and get the exception, what is wrong?

>>> foo = pexpect.spawn('scp root@192.168.126.11:/root/expect-5.42.1-1.i386.rpm /tmp')
>>> foo.logfile = sys.stdout
>>> foo.expect('.*password:')
root@192.168.126.11's password: 0
>>> foo.sendline('symqfct')
symqfct
8
>>> foo.interact()


expect-5.42.1-1.i386.rpm 100% 148KB 147.7KB/s 00:00

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/site-packages/pexpect.py", line 1062, in interact
self.__interact_copy(escape_character)
File "/usr/lib/python2.3/site-packages/pexpect.py", line 1086, in __interact_copy
data = self.__interact_read(self.child_fd)
File "/usr/lib/python2.3/site-packages/pexpect.py", line 1075, in __interact_read
return os.read(fd, 1000)
OSError: [Errno 5] Input/output error

Thanks
 
Old 03-02-2006, 10:36 PM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
As I said, I'm really not familiar with pexpect, I am only going by what I've read on the documentation page. One thing that springs to mind is that the process has ended, and the code is trying to read from a closed file... it certainly looks like it was working until the child process quit. I really can't help any more than that; you might be best to post a new question about pexpect so it appears on the 'no replies' list.
 
Old 03-02-2006, 10:56 PM   #8
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
Thank you very much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect AND print stdout samel_tvom Programming 12 10-07-2012 05:37 PM
redirect stdout to a varible Furlinastis Programming 3 12-07-2005 06:01 AM
how to redirect stdout [binary stream] to multiple processes vtaminh Linux - General 2 08-19-2004 01:05 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM
TCSH: redirect stdout and stderr seperately ugenn Linux - General 2 06-06-2002 12:07 PM


All times are GMT -5. The time now is 07:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration