【发布时间】:2016-01-04 15:58:56
【问题描述】:
我写了一个小代码,它扫描空中的 wifi 信号并根据数量播放一个或另一个音频文件。
import time
import subprocess
import os
while True:
output = subprocess.Popen("airport en1 -s", shell=True, stdout=subprocess.PIPE).stdout.read()
lines = output.split('\n')
n = len(lines)
if n < 10:
print os.path.exists('/Users/shirin/Desktop/wifi/1.mp3')
p = subprocess.Popen(['/Applications/VLC.app/Contents/MacOS/VLC','/Users/shirin/Desktop/wifi/1.mp3'])
elif n > 10:
print os.path.exists('/Users/shirin/Desktop/wifi/1.mp3')
p = subprocess.Popen(['/Applications/VLC.app/Contents/MacOS/VLC','/Users/shirin/Desktop/wifi/1.mp3'])
现在我的问题是,当我通过终端运行文件时,它会一遍又一遍地运行代码,我不知道如何停止它。当我关闭终端时它也不会停止。
所以我的问题:
如何告诉 python 运行一次代码然后停止?
已经非常感谢了!
【问题讨论】:
-
为什么
while True:? -
如果你只想运行一次,不要使用while循环
-
那么循环在这段代码中的什么位置?我将这段代码复制粘贴在一起,我知道,这从来都不聪明,但我是新手