【发布时间】:2021-08-22 18:13:17
【问题描述】:
您好,我在启动时运行 python 脚本时遇到了困难,该脚本要求用户在终端中输入以命名目录。 python 脚本询问文件名,然后创建一个目录和 csv 文件来存储图片和每张图片的信息,例如 csv 代码中的 GPS 数据。我的代码在 Geany 中运行时运行良好,但我尝试了所有可以想象的方式在启动时启动代码。如果我将direcname = str(input("name your file: ")) 更改为 direcname=str("file_name"),该代码将起作用。我花了几天的时间试图弄清楚这一点,我找不到一种在启动时打开终端的方式适用于我的脚本。
#import packages
from gpiozero import Button, LED
from picamera import PiCamera
import os
import datetime
from gps import *
#define gpio pins and variables
pwd = os.getcwd()
camera = PiCamera()
led = LED(13)
previewbtn = Button(26, hold_time=2)
counter = 1
#GPS stuff
gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)
#make new directory and create text file within
direcname = str(input("name your file: "))
newpath = pwd + '/' + direcname
os.makedirs(newpath)
txtfile = open(newpath + '/' + direcname + '.csv', 'w+')
txtfile.write('img, date/time, lat, lon, alt(m)')
txtfile.close()
#define functions
def capture():
global counter
camera.capture(newpath + '/' + direcname + str(counter) + '.jpg')
txtfile = open(newpath + '/' + direcname + '.csv', 'a')
txtfile.write("\n")
txtfile.write( direcname + str(counter) + ',' + str(datetime.datetime.now()) +
',' + lat1 + ',' + lon1 + ','+ alt1)
txtfile.close()
counter += 1
#run function
try:
while True:
#Setting lat,lon, and alt as variables
report = gpsd.next()
if report['class'] == 'TPV':
if getattr(report,'lat',0.0)!=0:
lat1 = str(getattr(report,'lat',0.0))
if getattr(report,'lon',0.0)!=0:
lon1 = str(getattr(report,'lon',0.0))
if getattr(report,'alt','nan')!= 'nan':
alt1 = str(getattr(report,'alt','nan'))
else:
lat1 = "ERROR"
lon1 = "ERROR"
alt1 = "ERROR"
#Everything else
led.source = previewbtn
previewbtn.when_pressed = camera.start_preview
previewbtn.when_held = capture
previewbtn.when_released = camera.stop_preview
except(KeyboardInterrupt, SystemExit):
print("Done.\nExiting")
【问题讨论】:
-
搜索 crontabs。我能够使用它在启动时自动运行 py 脚本。
-
@Goion 我试过了,但它不起作用,因为我需要使用终端来完成脚本
-
您能否更改脚本使其不依赖用户输入?将文件命名为“gps_”+
time.time() -
你为什么假设你可以在启动时运行这样的脚本?引导是,嗯......将所有东西引导到可用的状态。我认为python最早在启动完成后可用。
-
如果你使用 python 3
input()会自动返回一个字符串,你不需要用str()进行强制转换
标签: python linux raspberry-pi boot