LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
Search this Thread
Old 02-27-2008, 04:57 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Python: catch stdout and stderr from external command


After some Googling I found out how to use os.popen("command") to run a command and catch the stdout. Problem now is that I want to catch stderr as well and they need to be separated.

Is there anyone who knows how to do this? If it is too complex it will be enough to just ignore stdout and only catch stderr. The result should be that nothing goes to the screen and the script get the errors if there are any (maybe the error code as well, to determine how the execution went).

Any ideas? :)
 
Old 02-27-2008, 05:30 PM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
see popen2 or the subprocess module, the later is more powerful/flexible.

Last edited by angrybanana; 02-27-2008 at 05:32 PM.
 
Old 02-28-2008, 01:28 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by angrybanana View Post
see popen2 or the subprocess module, the later is more powerful/flexible.
Thanks!
 
Old 02-28-2008, 03:51 AM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Ok, so I've found subprocess to be very effective. Next problem, though, is how to use the value "returncode". It seems that it is of type NoneType. I am very new to Python so my question is how I can cast it to an integer or if there is any other way I can determine if the call was successful.

Here is my code so far:
Code:
p = subprocess.Popen(["ls -l qwesdf"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = p.stdout
stderr = p.stderr

print "stdout: " + stdout.readline()
print "stderr: " + stderr.readline()
The output works, I can print the first line fine. But I would not want to depend on however stderr is empty or not to determine if the call was successful.
 
Old 02-28-2008, 07:57 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
If you just want to list files in your directory, the os module will do the job. There's no need to use subprocess to make a system call to ls.
 
Old 02-28-2008, 09:19 AM   #6
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by Ephracis View Post
Next problem, though, is how to use the value "returncode". It seems that it is of type NoneType. I am very new to Python so my question is how I can cast it to an integer or if there is any other way I can determine if the call was successful.
"returncode
The child return code. A None value indicates that the process hasn't terminated yet. A negative value -N indicates that the child was terminated by signal N (Unix only)." - subprocess doc

Basically you have to let the process terminate.
p.wait() or p.communicate() would do the trick. Be careful with communicate() if the data is is big, it returns a tuple with two strings (stdout, stderr)

Code:
p.wait()
# returncode will now have a value
p.returncode
This is of course if you're not just doing 'ls -l', if that's all you need then follow ghostdog's advice.

Last edited by angrybanana; 02-28-2008 at 09:22 AM.
 
Old 02-28-2008, 09:22 AM   #7
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by ghostdog74 View Post
If you just want to list files in your directory, the os module will do the job. There's no need to use subprocess to make a system call to ls.
It was just a quick example to create output to stderr.

If you are interested I will actually run g++/gcc and tar, not ls.
 
Old 02-28-2008, 09:25 AM   #8
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by angrybanana View Post
This is of course if you're not just doing 'ls -l', if that's all you need then follow ghostdog's advice.
If all I wanted to do was ls I would probably just do a one-liner or in worst case a shell-script.

Anyway, thanks for the tips. I will try them out right now. :)
 
Old 02-28-2008, 09:26 AM   #9
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by Ephracis View Post
It was just a quick example to create output to stderr.

If you are interested I will actually run g++/gcc and tar, not ls.
Just throwing it out there.. module-tarfile
 
Old 02-28-2008, 09:57 AM   #10
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by angrybanana View Post
Just throwing it out there.. module-tarfile
Haha, cool! Now if there is just a module-compiler I don't need any external system calls. :P
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
send stderr to a file and also to stdout pranavchoudhary Linux - Newbie 7 08-25-2008 02:57 AM
python: how do you capture stdout stream from external program? BrianK Programming 8 01-24-2008 09:32 PM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 04:15 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 03:36 AM
Reopen the stdout and stderr rahul_kulkarni Programming 3 02-21-2005 07:55 AM


All times are GMT -5. The time now is 04:51 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