本文介绍了os.system python 函数在 while 循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个简单的 python 脚本来在 ubuntu 14.04 中自动点击.
I am trying to make a simple python script to automatically click in ubuntu 14.04.
这是我的代码
#!/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
推荐答案
如果你仍然需要使用 os.system
你应该这样做:
If you still need to use os.system
you should do this:
timeNow = os.popen('date +\"%s\"').read()
更好的方法是使用 subprocess
:
import subprocess
proc = subprocess.Popen(('date +\"%s\"'.split(), stdout=subprocess.PIPE, shell=True)
(timeNow, err) = proc.communicate()
但如评论中所述 - 在您的情况下使用 time
But as stated in comments - in your case use time
这篇关于os.system python 函数在 while 循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!