os.system python function in a while loop

1.9k views Asked by At

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

2

There are 2 answers

0
Reut Sharabani On BEST ANSWER

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

0
hariK On

os.system returns exit status. If you need to get the command's output to a variable try,

import commands

import os

clickCounter = 0

while clickCounter == 0:

timeNow = commands.getoutput('date +\"%s\"')

if timeNow > 10:       

    os.system('xdotool click 1')

    clickCounter = clickCounter + 1