如何使用python编写树莓派的自启动目录?

发布于 2025-01-15 13:51:48 字数 1656 浏览 0 评论 0原文

我正在使用带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2025-01-22 13:51:48

我有一种预感,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文