如何使用python编写树莓派的自启动目录?
我正在使用带有 Buster 操作系统的 Raspberry Pi4 Model B。我正在尝试编写一个 python 脚本来在启动时运行网页。在终端中输入 sudo nano /etc/xdg/lxsession/LXDE-pi/autostart 并添加 @chromium-browser --start-fullscreen --start-maximized https://teams.microsoft.com/ 在最后一行我可以在启动时看到网页。但问题是我需要用 python 程序来做到这一点。尝试了很多脚本,但都不起作用。每次它都会抛出错误“没有这样的文件或目录:sudo nano /etc/xdg/lxsession/LXDE-pi/autostart”。我附上下面的脚本。请帮我解决这个问题,我是 Raspberry Pi 的新手。
不添加 sudo nano
conf_file = /etc/xdg/lxsession/LXDE-pi/autostart
def update_url():
conf_file= '/etc/xdg/lxsession/LXDE-pi/autostart'
try:
with open(conf_file, 'w')as file:
file.write('@chromium-browser --start-fullscreen --start-maximized https://teams.microsoft.com/')
except Exception as ex:
print("Cant write dirctory:",ex)
return 0
finally:
pass
输出:
permission returned: /etc/xdg/lxsession/LXDE-pi/autostart
添加 sudo nano
conf_file = sudo nano / etc/xdg/lxsession/LXDE-pi/autostart
def update_url():
conf_file= 'sudo nano /etc/xdg/lxsession/LXDE-pi/autostart'
try:
with open(conf_file, 'w')as file:
file.write('@chromium-browser --start-fullscreen --start-maximized https://teams.microsoft.com/')
except Exception as ex:
print("Cant write dirctory:",ex)
return 0
finally:
pass
输出:
没有这样的文件或目录: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
I am using Raspberry Pi4 Model B with buster os in it. I am trying to write a python script for running the webpage at bootup. On entering sudo nano /etc/xdg/lxsession/LXDE-pi/autostart in the terminal and adding @chromium-browser --start-fullscreen --start-maximized https://teams.microsoft.com/ at the last line I can see the webpage at boot. But the problem is I need to do this with a python program. Have tried many scripts and it's not working. Every time It's throwing the error saying "No such file or directory: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart". I am attaching the script below. Please help me in solving this I am new to Raspberry Pi.
Without adding sudo nano
conf_file = /etc/xdg/lxsession/LXDE-pi/autostart
def update_url():
conf_file= '/etc/xdg/lxsession/LXDE-pi/autostart'
try:
with open(conf_file, 'w')as file:
file.write('@chromium-browser --start-fullscreen --start-maximized https://teams.microsoft.com/')
except Exception as ex:
print("Cant write dirctory:",ex)
return 0
finally:
pass
Output:
permission denied: /etc/xdg/lxsession/LXDE-pi/autostart
Adding sudo nano
conf_file = sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
def update_url():
conf_file= 'sudo nano /etc/xdg/lxsession/LXDE-pi/autostart'
try:
with open(conf_file, 'w')as file:
file.write('@chromium-browser --start-fullscreen --start-maximized https://teams.microsoft.com/')
except Exception as ex:
print("Cant write dirctory:",ex)
return 0
finally:
pass
Output:
No such file or directory: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一种预感,Python 认为“sudo nano”是一个目录。也许尝试让它在终端中运行。阅读以下内容:将 sudo 与 Python 脚本结合使用
I have a hunch that Python thinks that "sudo nano" is a directory. Maybe attempt to make it run in the terminal. Read this: Using sudo with Python script