0

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

  • 4
    [`os.system`](https://docs.python.org/2/library/os.html#os.system) returns the *exit status* of the command, not its standard output. And for the love of `$DEITY`, don't shell out to `date` to get the time! There's the entire [`time`](https://docs.python.org/2/library/time.html#time.time) module! – Jonathon Reinhart Dec 14 '14 at 00:16
  • @JonathonReinhart Thank you, i can't believe i forgot about time.time() – Owen Boucher Dec 14 '14 at 00:23

2 Answers2

-1

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

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
-1

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
hariK
  • 2,722
  • 13
  • 18