Search code examples
pythonubuntuos.systemxdotool

os.system python function in a while loop


I am trying to make a simple python script to automatically click in ubuntu 14.04.

here is my code

#!/usr/bin/python
import os
clickCounter = 0
while clickCounter == 0:
    timeNow = os.system('date +\"%s\"')
    if timeNow > 10:
        os.system('xdotool click 1')
        clickCounter = clickCounter + 1

however, for some reason, all it will do is print out the time again and again until i close the terminal. if anyone can help me it would be very appreciated


Solution

  • If you still need to use os.system you should do this:

    timeNow = os.popen('date +\"%s\"').read()
    

    A better way is using subprocess:

    import subprocess
    proc = subprocess.Popen(('date +\"%s\"'.split(), stdout=subprocess.PIPE, shell=True)
    (timeNow, err) = proc.communicate()
    

    But as stated in comments - in your case use time