From: Raghuram D. <ra...@sy...> - 2006-06-29 20:27:15
|
You can do something like os.system("ls -l >/tmp/testfile"). You may want to generate a temp file name and delete it after reading it. Note that readng stdout/err is very tricky in java as well. Dong, Ming wrote: > When run os.popen("ls","r"), I got error message like: > > AttributeError: class 'org.python.modules.os' has no attribute 'popen' > > I am using jython 2.1, any suggestions? > > Thanks a lot! > > Ming Dong > > -----Original Message----- > From: jyt...@li... > [mailto:jyt...@li...] On Behalf Of Dave > Kuhlman > Sent: Thursday, June 29, 2006 11:57 AM > To: jyt...@li... > Subject: Re: [Jython-users] Help: how to get the os.system() stdout > > On Thu, Jun 29, 2006 at 11:22:21AM -0400, Dong, Ming wrote: >> >> Hi, >> >> I want to write the output of the os.system() to a log file, but >> don't know how to read the output to a string. Could you give me some >> suggestion? > > You want one of the variants of popen. See: > > http://docs.python.org/lib/os-newstreams.html > http://docs.python.org/lib/module-popen2.html > > Here is an example: > > def test1(): > outstream = os.popen('ls *.py', 'r') > for line in outstream: > print line.rstrip() > > The other versions (popen2, popen3, ...) enable you to do things > like pipe input to the command and read its output, read from > stderr as well as stdout, etc. > > Dave > > |