7

I have been playing with Python for almost five days and I honestly enjoy it.
I have this challenge and I couldn't solve it.
The challenge is to repeat the output of top command every 10 seconds and save it into a file.
Here is what I have done so far.

import time, os, threading

def repeat():
    print(time.ctime())
    threading.Timer(10, repeat).start()
    f = open('ss.txt', 'w')
    top = os.system("sudo top -p 2948")
    s = str(top)
    text = f.write(s)
    print(text)

repeat()
2
  • 5
    please format your code properly Commented Jan 21, 2013 at 10:21
  • 3
    Have you found any way to get top output instead of calling command?? I mean using any system libs or python libs??? Commented Aug 25, 2017 at 18:57

4 Answers 4

6

The main problem here is that a call to top does not terminate immediately but runs continuously in a loops to display new data. You can change this behaviour by specifying the -n1 option (-n allows you to specify the number of iterations).

Try something like this:

import subprocess

## use the following where appropriate within your loop
with open("ss.txt", "w") as outfile:
  subprocess.call("top -n1 -p 2948", shell=True, stdout=outfile)
Sign up to request clarification or add additional context in comments.

Comments

1

It is advisable to use subprocess for invoking another process. You need to pass the file object of the file where you want to write output. e.g.

    import time, os, threading, subprocess
    def repeat():
      print(time.ctime())
      threading.Timer(10, repeat).start()
      with open('ss.txt', 'w') as f:
          subprocess.call(["sudo","top","-p","2948"],stdout=f)

This should save the output of command to a file which you can read later.

2 Comments

I tried this on my system and output is being written to file pefectly. What error do you see?
subprocess.call("sudo top -n1 -p 2948", shell=True, stdout=f) by specifying the -n1 option (-n allows you to specify the number of iterations). thanks man for your help anyway.
0

You can also use the time.sleep() function, and wait for 10 seconds before continuing.
Not sure whether this is what you want...

import time,os

def repeat(seconds,filename):
    while True:
        print(time.ctime())
        f = open(filename, 'w')
        top = os.system("sudo top -p 2948")
        s = str(top)
        time.sleep(seconds)
        f.write(s)

repeat(5,'ss.txt')

Also

  1. f.write returns None, as it writes on the file, not returning any value, so it is useless storing that value.
  2. check out PEP 324, it notes the features of subprocess module. (thx to @ajm)
  3. subprocess.Popen() has a lot of functionality(tools), so it can replace many other 'tools', (see here) so you may consider that too.

Comments

-1

First, your code is not formated properly, it should look more like that:

import time, os, threading

def repeat():
  print(time.ctime())
  threading.Timer(10, repeat).start()
  f= open('ss.txt', 'w')
  top= os.system("sudo top -p 2948")
  s=str(top)
  text = f.write(s)
  print text

repeat()

Then, you might want to look into subprocess module - its the more modern and kosher way to call external commands than os.system. However, if your code works, what's the actual problem?

2 Comments

the problem is i can't save it into a file.
Ok, in such case can you clarify your initial post with details on what the issue is, what errors are you getting and so on?

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.