从 if [Python] 退出
Exit from an if [Python]
如果构造
我无法退出
#!/usr/bin/python2.7
import os
import re
import time
global state
#print status_on.group()
def main():
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if status_on:
state = status_on
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
if status_on == state:
print "True"
state = 1 #here i can't exit
if status_on != state:
print state
os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")
while True:
main()
我试过通过,但是当我执行脚本时,它显示为输出:
True
1
True
1
等等。
如果我使用 "break" 它会崩溃。
这应该有效。将您的全局声明移动到主函数内部,然后这应该初始化状态,然后检查状态变化。这可能可以稍微清理一下(即 if 语句),但它应该可以工作。
#!/usr/bin/python2.7
import os
import sys
import re
import time
#print status_on.group()
def main():
global state
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if (status_on is None and state) or (state is None and status_on):
state = status_on
if status_on:
print "a jack was plugged in"
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
else:
print "a jack was unplugged"
os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
state = re.search('values=on', output)
while True:
main()
然后使用保存先前状态的变量。并且这样做:
if status_on == state and previousState!=1:
print "True"
state = 1
如果构造
我无法退出#!/usr/bin/python2.7
import os
import re
import time
global state
#print status_on.group()
def main():
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if status_on:
state = status_on
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
if status_on == state:
print "True"
state = 1 #here i can't exit
if status_on != state:
print state
os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")
while True:
main()
我试过通过,但是当我执行脚本时,它显示为输出:
True
1
True
1
等等。 如果我使用 "break" 它会崩溃。
这应该有效。将您的全局声明移动到主函数内部,然后这应该初始化状态,然后检查状态变化。这可能可以稍微清理一下(即 if 语句),但它应该可以工作。
#!/usr/bin/python2.7
import os
import sys
import re
import time
#print status_on.group()
def main():
global state
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if (status_on is None and state) or (state is None and status_on):
state = status_on
if status_on:
print "a jack was plugged in"
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
else:
print "a jack was unplugged"
os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
state = re.search('values=on', output)
while True:
main()
然后使用保存先前状态的变量。并且这样做:
if status_on == state and previousState!=1:
print "True"
state = 1