启动时自动启动 python 脚本 (RPi) [英] Auto Starting a python script on boot (RPi)

查看:64
本文介绍了启动时自动启动 python 脚本 (RPi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 RPi 上有一个需要在启动时运行的 Python 脚本

I have a python script on my RPi that needs to run on boot

我将它添加到 rc.local,它曾经工作正常

I added it to rc.local, and it used to work fine

几天前,我在程序中添加了一个功能,现在它使用 open() 读取 txt 文件

A few days ago, I added a functionality to the program, and it now uses open() to read a txt file

现在每次我重新启动 Pi 时,python 都会给我一个错误:

Now every time I restart the Pi, python gives me and error:

File "home/pi/client.py", line 13, in <module>
    stats=open('stats.txt')
IOError: [Errno 2] No such file or directory: 'stats.txt'

当我手动启动脚本时:

sudo python client.py

它运行良好,没有问题.

it works fine with no problems.

有什么建议吗?

推荐答案

您的 rc.local 可能没有在正确的目录中启动您的脚本.所以你应该:

Your rc.local probably does not start your script in the correct directory. So you should either:

  • 使用类似 cd my/dir &&python/path/to/home/pi/client.py
  • 在脚本中调用 os.chdir("/path/to/some_dir")
  • 打开文件时使用绝对路径:stats = open('/path/to/stats.txt')

这篇关于启动时自动启动 python 脚本 (RPi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆