Python 2.7 停止python中os.system()启动的进程
我目前正在使用raspberry pi制作一个数据记录系统,我正在使用一个按钮来启动/停止数据记录。我能够通过os.system()执行python脚本“3ldrdatalog.py”,但我找不到在第二次按下按钮时停止脚本的特定命令 请帮我写代码! 提前谢谢Python 2.7 停止python中os.system()启动的进程,python-2.7,os.system,Python 2.7,Os.system,我目前正在使用raspberry pi制作一个数据记录系统,我正在使用一个按钮来启动/停止数据记录。我能够通过os.system()执行python脚本“3ldrdatalog.py”,但我找不到在第二次按下按钮时停止脚本的特定命令 请帮我写代码! 提前谢谢 import RPi.GPIO as GPIO from Adafruit_CharLCD import Adafruit_CharLCD GPIO.setmode(GPIO.BCM) GPIO.setup(13,GPIO.IN) inpu
import RPi.GPIO as GPIO
from Adafruit_CharLCD import Adafruit_CharLCD
GPIO.setmode(GPIO.BCM)
GPIO.setup(13,GPIO.IN)
input = GPIO.input(13)
import time
import os
lcd = Adafruit_CharLCD()
#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 1
try:
lcd.message('Press button')
while True:
#take a reading
input = GPIO.input(13)
#if the last reading was low and this one high, print
if ((not prev_input) and input):
print("Button pressed")
lcd.clear()
#this is the script that will be called (as root)
os.system("python /home/pi/3ldrdatalog.py")
#update previous input
prev_input = input
#slight pause to debounce
time.sleep(0.05)
except KeyboardInterrupt:
file.close()
pass
finally:
GPIO.cleanup()
为什么不使用subprocess.popen呢?我可以打开python脚本并以root用户身份运行它,但我想终止python脚本,该脚本将添加到下一个if语句中。尝试改用sub=subprocess.popen(“python/home/pi/3ldrdatalog.py”),但是得到了错误作为回溯(最近一次调用是最后一次):文件“/home/pi/subprocesspush.py”,第22行,在sub=subprocess.Popen(“python/home/pi/3ldrdatalog.py”)文件“/usr/lib/python2.7/subprocess.py”,第710行,在init errread中,errwrite)文件“/usr/lib/python2.7/subprocess.py”,第1335行,在_execute_child raise child_exception OSError:[Errno 2]没有这样的文件或目录谢谢,我找到了解决方案:)