1

So when I run subprocess.call in python, after running the script, if there are error messages caused by the bash, I would like to not display it to the user.
So for instance,

for i in range(len(official_links)):
    if(subprocess.call('pacman ' + '-Qi ' + official_links[i].replace('https://www.archlinux.org/packages/?q=', ''),shell=True, stdout=subprocess.PIPE) == 0):
        print(official_links[i].replace('https://www.archlinux.org/packages/?q=', '') + ' installed')
    else:
        print(official_links[i].replace('https://www.archlinux.org/packages/?q=', '') + ' not installed')

the command pacman -Qi packagename cheks if the packagename is already installed or not. When I run my script, if it is installed, I get no extra messages from the bash, only what I print. But if the package does not exist and an error is caused, both the error and my print gets printed on the screen.

Is there a way to avoid printing command errors too?

Thanks.

1 Answer 1

2

Redirect the stderr as well:

if(subprocess.call('pacman ' + '-Qi ' + official_links[i].replace('https://www.archlinux.org/packages/?q=', ''),shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0):

That's where the error is displayed.

Sign up to request clarification or add additional context in comments.

1 Comment

That did the job! As a bonus question, may I ask if using this technique to avoid messages is prone to any specific problems? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.