bluethundr
bluethundr

Reputation: 1335

Send screen output to /dev/null in Python

I am returning a variable from a function in python.

When I try to retrieve the variable in another function, it prints out the print statements of the original function for a second time.

Here is my code:

user_name = 'my_user'
kms_cleint = 'client_info'
aws_account = 'company-account'
def create_kms_key(user_name, kms_client):   
    print("****************************************************************")
    print("         Create KMS Key for %s                                   " % user_name)
    print("****************************************************************")
    kms_key_id = 5
    return kms_key_id

def store_secret(user_name, kms_client, secrets_client, aws_account):
    print("****************************************************************")
    print("         Store Secret for %s for AWS account: %s                " % (user_name, aws_account))
    print("****************************************************************")
    f = open(os.devnull,"w")
    kms_key_id = create_kms_key(user_name, kms_client).set_log_stream(f)

My output:

****************************************************************
         Create KMS Key for user35
****************************************************************
****************************************************************
         Store Secret for user35 for AWS account: company-account
****************************************************************
****************************************************************
         Create KMS Key for user35
****************************************************************

I am trying to avoid printing out the create_kms_key function output a second time.

I am getting this error when I try to redirect the output to /dev/null when I call the create_kms_key function:

AttributeError: 'int' object has no attribute 'set_log_stream'

How can I keep the create_kms_key function from printing it's output a second time?

Upvotes: 2

Views: 1058

Answers (2)

bluethundr
bluethundr

Reputation: 1335

I solved the problem by doing this:

import contextlib
    with contextlib.redirect_stdout(None):
        kms_key_id = create_kms_key(user_name, kms_client)

That prevents the function from printing out a second time.

Upvotes: 3

J-L
J-L

Reputation: 1901

The error has to do with this line:

create_kms_key(user_name, kms_client).set_log_stream(f)

The function create_kms_key returns an int, which then you immediately call .set_log_stream(f), which is not a method of int. Not only that, but create_kms_key runs completely before .set_log_stream(f) gets invoked.

Instead of these lines:

f = open(os.devnull,"w")
kms_key_id = create_kms_key(user_name, kms_client).set_log_stream(f)

try these instead:

with open(os.devnull, 'w') as devnull:
    saved_stdout = sys.stdout  # save off the real stdout
    sys.stdout = devnull       # temporarily suppress stdout
    kms_key_id = create_kms_key(user_name, kms_client)
    sys.stdout = saved_stdout  # restore stdout

This way you'll temporarily suppress the text normally written to sys.stdout. You'll have to be careful about any exceptions thrown in create_kms_key, as they could skip over the restoration line of code, preventing sys.stdout from being properly restored.

Upvotes: 2

Related Questions