I am trying to open and edit an executable linux script on a Pi, what I have is something like this:

import os
os.system("sudo nano /etc/rc.local")

then I want to edit the content, then save the file and reboot the Pi any suggestions?

| improve this question | | | | |
  • You are obviously new to lots of things. It makes no sense to run a python script to open an editor. Just enter sudo nano /etc/rc.local at the command line. This is not a discussion site. You need to read some basics - You might try raspberrypi.org/magpi/issues/essentials-bash-vol1 and read some of the MagPI issues which have tutorials. – Milliways Oct 13 '16 at 9:28
  • Are you trying to use Python as terminal shell? – Dmitry Grigoryev Oct 14 '16 at 9:37
  • Thanks milliways, I know how terminal works, but I cannot access that from the Webpage that I have that is running on my Linux environment that is written in Python. – Johan Oct 14 '16 at 11:37
  • Why are you trying to do this from the web? Why are you trying to do this from the web? This is a large security issue. – Steve Robillard Oct 15 '16 at 0:18

The L Pee's answer is the best way to edit the file. However if you are using python and, for example, you want a script to run on boot under a set of circumstances already determined by your python script, then one method I saw somewhere on the internet which avoids root issues is...

Read the original file:

with open('etc/rc.local', 'rt') as original_rc_file:
    output_rc_file = original_rc_file.read()

You can then edit output_rc_file as a string, rc.local needs an "exit 0" at the end I think.

Next you save it somewhere temporary you have access to and 'sudo mv' it on top of the original (probably best to back up the original first?).

Save your new file with:

with open ('/tmp/output_rc_file.txt', 'wt') as output_file:

Then copy over the original with:

os.system('sudo mv /tmp/output_rc_file.txt /etc/rc.local')
| improve this answer | | | | |
  • Thanks Craig this is very helpful, exactly what I am looking for. – Johan Oct 14 '16 at 11:34
  • Glad it helps. Since writing I've come across a minor issue. mv tries to preserve as much as the file meta data as possible. This may not be a good thing in some cases. This post [unix.stackexchange.com/questions/131180/… shows how cp and rm may be a safer option. – Craig Oct 18 '16 at 9:54
  • "The above answers" are now below! – uhoh Mar 14 '18 at 8:36

I'd go with the following command at the $ prompt, where the '/etc/rc.local' represents the path to the file and the file name,

    sudo nano /etc/rc.local

Make your changes then to exit

    <ctl> x 

'y' and Return to save the file.


    sudo reboot

Alternatively if you prefer to use a text editor I use Leafpad which is already installed and can be found via the main menu in 'accessories'.

Take a look here for some more detailed help for Leafpad http://www.dummies.com/computers/raspberry-pi/how-to-use-the-leafpad-text-editor-on-your-raspberry-pi/

| improve this answer | | | | |
  • Hi L Pee, I am not using terminal, using Python. – Johan Oct 14 '16 at 11:38
  • Hi there, what do you mean by 'using Python'? The above code is using the Python language on the command line. I'm afraid I don't know of a text editor called Python. – L Pee Oct 15 '16 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.