answerSeeker
answerSeeker

Reputation: 2772

How to exclude a specific message from stdout when using apt in python script?

I get this message: "WARNING: apt does not have a stable CLI interface. Use with caution in scripts."

In a simple script that does

process = subprocess.Popen(['apt', 'upgrade', '-y'], stdout=subprocess.PIPE)

How can I remove that annoying warning when running apt from python subprocess? I don't mind the rest of the output, I only want to get rid of the warning.

Edit:

I'm iterating though the output so that I can get live output from apt. How can I remove it from there?

with open('test.log', 'w') as f:
        process = subprocess.Popen(['apt', 'upgrade', '-y'],
                                   stdout=subprocess.PIPE,  stderr=subprocess.PIPE)
        for line in iter(process.stdout.readline, ''):
                sys.stdout.write(line)
                f.write(line)

Upvotes: 1

Views: 350

Answers (1)

Blender
Blender

Reputation: 298392

That message is sent over stderr. I would capture it by piping it, just like you did with stdout:

>>> process = subprocess.Popen(['apt', 'search', 'python'], stdout=subprocess.PIPE,
...                                                         stderr=subprocess.PIPE)
>>> stdout, stderr = process.communicate()
>>> stderr
'\nWARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n\n'

Upvotes: 1

Related Questions