无论如何可以在启动时运行打开终端的 python 脚本吗?



您好,我很难在启动时运行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") 

如果您使用的是窗口管理器,则可以在.xinitrc中添加python filename.py &,这将开始执行在该脚本中编写的任何内容。

如果使用任何桌面环境,那么它带有一个默认的显示管理器,该管理器可以执行相同的操作。每个显示管理器都有自己的自动启动方式。

我建议在登录时启动它,而不是在启动时启动。

最新更新