Python3 devnull and stderr

This is the place for queries that don't fit in any of the other categories.

Python3 devnull and stderr

Postby DevynCJohnson » Sun May 26, 2013 1:56 pm

For any given command, how do I send the stderr of a particular command to os.devnull (or subprocess.devnull)? For example, how do I send the stderr of this command (if an error does occur) to devnull? I am using Python3.x code.

Code: Select all
os.remove('./mem/INPUTLIST.db'); os.remove('./mem/THATLIST.db')
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Python3 devnull and stderr

Postby setrofim » Sun May 26, 2013 2:56 pm

Code: Select all
import os

paths =  ['./mem/INPUTLIST.db', './mem/THATLIST.db']
for path in paths:
    try:
        os.remove(path)
    except OSError:
        pass
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Python3 devnull and stderr

Postby casevh » Sun May 26, 2013 2:59 pm

Here's an example for subprocess:

Code: Select all
>>> import subprocess
>>> subprocess.call(['rm', 'foo.bar'])
rm: cannot remove `foo.bar': No such file or directory
1
>>> subprocess.call(['rm', 'foo.bar'], stderr = subprocess.DEVNULL)
1
>>>


BTW, os.remove() doesn't send any output to stderr.

casevh
casevh
 
Posts: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: Python3 devnull and stderr

Postby DevynCJohnson » Sun May 26, 2013 3:10 pm

Thank you everyone for your answers. I never knew for sure whether or not os.remove printed stderr. The command
Code: Select all
subprocess.call(['rm', 'foo.bar'], stderr = subprocess.DEVNULL)
is very helpful. I like to keep my coding simple and short.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests