2

I was trying to capture output of top command using the following python script:

    import os
    process = os.popen('top')
    preprocessed = process.read()
    process.close()
    output = 'show_top.txt'
    fout = open(output,'w')
    fout.write(preprocessed)
    fout.close()

However, the script does not work for top. It gets stuck for a long time. However it works well with commands like 'ls'. I have no clue why this is happening?

2 Answers 2

1

Since you're waiting for the process to finish, you need to tell top to only print its output once, and then quit.

You can do that by running:

top -n 1
Sign up to request clarification or add additional context in comments.

Comments

1

-b argument required when stdout read from python

os.popen('top -b -n 1')

top -b -n 1

1 Comment

This is the right one. Finally after searching SO for so long...

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.