How to write the Raspberry Pi's autostart directory using python?

618 Views Asked by At

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

1

There are 1 best solutions below

1
On

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