We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Python files not saving

Wed Feb 16, 2022 6:48 pm

Whenever I create a python script in the terminal and tell it to save it, the files get saves as "shutdown.py.save" intstead of "shutdown.py" like I want.

What am I doing wrong?

ras_oscar
Posts: 217
Joined: Thu Jan 06, 2022 4:31 pm

Re: Python files not saving

Thu Feb 17, 2022 3:11 am

what editor are you using?
I had a similar problem writing Python with notepad. I was telling it to save as myprogram.py and it ended up adding its own extension and saving as myprogram.py.txt. I'm now using thonny and haven't seen the problem since

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Re: Python files not saving

Thu Feb 17, 2022 5:50 pm

I just opened the terminal and entered this:

nano ~pi/Scripts/shutdown-with-hold.py

and then exited and saved the file. Do I need to be using an editor instead of this?

User avatar
rpiMike
Posts: 3603
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Python files not saving

Thu Feb 17, 2022 6:17 pm

What OS are you using? Are you running a desktop?

Why not use a GUI based Python editor such as Thonny?

User avatar
B.Goode
Posts: 18715
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python files not saving

Thu Feb 17, 2022 6:27 pm

BeJuice wrote:
Thu Feb 17, 2022 5:50 pm
I just opened the terminal and entered this:

nano ~pi/Scripts/shutdown-with-hold.py

and then exited and saved the file. Do I need to be using an editor instead of this?

What Operating System are you running on your RPi board?

Have you deliberately created a Scripts subdirectory in your Home directory? The nano editor will not create it if it does not exist. It is not a default location pre-installed with RasPiOS.


Code: Select all

pi@rpitest64:~ $ mkdir Scripts
pi@rpitest64:~ $ nano ~pi/Scripts/shutdown-with-hold.py
pi@rpitest64:~ $ ls -al Scripts
total 12
drwxr-xr-x  2 pi pi 4096 Feb 17 18:26 .
drwxr-xr-x 24 pi pi 4096 Feb 17 18:23 ..
-rw-r--r--  1 pi pi   22 Feb 17 18:26 shutdown-with-hold.py
pi@rpitest64:~ $ cat Scripts/shutdown-with-hold.py
# shutdown-with-hold

pi@rpitest64:~ $
Beware of the Leopard

User avatar
rpdom
Posts: 25187
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: Python files not saving

Thu Feb 17, 2022 6:56 pm

nano will add ".save" to the file name if it encounters a serious error such as running out of memory, or if it is killed with sighup.
Unreadable squiggle

klricks
Posts: 8928
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA

Re: Python files not saving

Thu Feb 17, 2022 7:45 pm

rpdom wrote:
Thu Feb 17, 2022 6:56 pm
nano will add ".save" to the file name if it encounters a serious error such as running out of memory, or if it is killed with sighup.
This can easily happen if the terminal window is closed by clicking the [X] without first saving the nano file.
3B+ & 4B4G Running RPi OS Bookworm w/ Desktop

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Re: Python files not saving

Thu Feb 17, 2022 9:16 pm

rpiMike wrote:
Thu Feb 17, 2022 6:17 pm
What OS are you using? Are you running a desktop?

Why not use a GUI based Python editor such as Thonny?
I am running a modified version of Raspbian OS I believe, it is from OpenAuto Pro. Its a program for a CarPi.

As to why I'm not using Thonny, I was following third part directions.

I have an external board that will set a GPIO pin to ground and tell the pi to shutdown by utilizing the script I am trying to write. If thonny can write a script then I will use that.

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Re: Python files not saving

Thu Feb 17, 2022 9:17 pm

B.Goode wrote:
Thu Feb 17, 2022 6:27 pm
BeJuice wrote:
Thu Feb 17, 2022 5:50 pm
I just opened the terminal and entered this:

nano ~pi/Scripts/shutdown-with-hold.py

and then exited and saved the file. Do I need to be using an editor instead of this?

What Operating System are you running on your RPi board?

Have you deliberately created a Scripts subdirectory in your Home directory? The nano editor will not create it if it does not exist. It is not a default location pre-installed with RasPiOS.


Code: Select all

pi@rpitest64:~ $ mkdir Scripts
pi@rpitest64:~ $ nano ~pi/Scripts/shutdown-with-hold.py
pi@rpitest64:~ $ ls -al Scripts
total 12
drwxr-xr-x  2 pi pi 4096 Feb 17 18:26 .
drwxr-xr-x 24 pi pi 4096 Feb 17 18:23 ..
-rw-r--r--  1 pi pi   22 Feb 17 18:26 shutdown-with-hold.py
pi@rpitest64:~ $ cat Scripts/shutdown-with-hold.py
# shutdown-with-hold

pi@rpitest64:~ $
I believe I am running a modified Raspbian OS from OpenAuto Pro. This pi is for a carpi/headunit.

I did create a folder called scripts.

User avatar
rpiMike
Posts: 3603
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Python files not saving

Thu Feb 17, 2022 9:22 pm

I was following third part directions
Are these directions shareable?

User avatar
B.Goode
Posts: 18715
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python files not saving

Thu Feb 17, 2022 9:37 pm

BeJuice wrote:
Thu Feb 17, 2022 9:17 pm

I did create a folder called scripts.



... but that is not where you are trying to create or edit the python file.



If you are following instructions related to a customised OS from a third-party maybe your support questions should be directed to them?

(Because chances are most volunteer helpers here will not know how that OS differs from the standard Raspberry Pi Operating System released directly from Raspberry Pi.)



https://bluewavestudio.io/contact/
Beware of the Leopard

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Re: Python files not saving

Thu Feb 17, 2022 10:16 pm

rpiMike wrote:
Thu Feb 17, 2022 9:22 pm
I was following third part directions
Are these directions shareable?
Yes, I will paste them below:

Python script install:
1.Create folder for script(s):
1. Open the terminal on your Raspberry and make folder Scripts:
- mkdir Scripts and press <Enter>

2.Create the script:
1. Open the terminal and enter:
- nano ~pi/Scripts/shutdown-with-hold.py and press <Enter>
- enter the code:

#!/usr/bin/env python3
from gpiozero import Button
from signal import pause
import os, sys
offGPIO = 21
holdTime = 2
# the function called to shut down the RPI
def shutdown():
os.system("sudo poweroff")
btn = Button(offGPIO, hold_time=holdTime)
btn.when_held = shutdown
pause() # handle the button presses in the background

- press Ctrl+X to close window
- press Y to save changes
or
open the terminal and go to the folder Scripts:
- cd Scripts and press <Enter>
- wget
https://raw.githubusercontent.com/picot ... 1_script/m
aster/shutdown-with-hold.py and press <Enter>

3.Test script:
1. Make shutdown-with-hold.py executable:
- chmod a+x shutdown-with-hold.py
- start script with ./shutdown-with-hold.py
- connect GPIO21 to GND for 2 seconds and check that Pi will start to the shutdown
If this is correct, go to step 4.

4.Make script run at boot time:
1. Open the terminal and enter:
- sudo su and press <Enter>
- nano /etc/rc.local and press <Enter>
- at the end of the file but before exit 0 write:
python /home/pi/Scripts/shutdown-with-hold.py &

∙ NOTE: there is one space before &
∙ press Ctrl+X to close window
∙ press Y to save changes
∙ reboot Raspberry

I am currently stuck at step 3, since my code is saved as "shutdown-with-hold.py.save" instead of "shutdown-with-hold.py".

Thank all of you for your help!
I'm in a little over my head.

User avatar
B.Goode
Posts: 18715
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python files not saving

Thu Feb 17, 2022 11:24 pm

If you look back you will see that I demonstrated those steps being carried out successfully.


Previously you said you had created a directory scripts. Which is not what the instructions say...

What did you actually do?



What is the output from -

Code: Select all

 ls -al Scripts
Beware of the Leopard

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Re: Python files not saving

Fri Feb 18, 2022 1:25 am

B.Goode wrote:
Thu Feb 17, 2022 11:24 pm
If you look back you will see that I demonstrated those steps being carried out successfully.


Previously you said you had created a directory scripts. Which is not what the instructions say...

What did you actually do?



What is the output from -

Code: Select all

 ls -al Scripts
The output is:

total 16
drwxr-xr-x 2 pi pi 4096 Jul 27 21:56 .
drwxr-xr-x 25 pi pi 4096 Jul 27 22:22 ..
-rw------- 1 pi pi 242 Jul 27 21:56 shutdown-with-hold.py.save

User avatar
B.Goode
Posts: 18715
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python files not saving

Fri Feb 18, 2022 9:03 am

The saved file doesn't look big enough to contain the python script. (You could check by following my example, above.)

I suggest you delete it and recreate it.

If cating the file shows that it is intact you could simply make a copy with the correct name.

Something like -

Code: Select all

cp ~/Scripts/shutdown-with-hold.py.save  ~/Scripts/shutdown-with-hold.py
Beware of the Leopard

bjtheone
Posts: 3353
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Python files not saving

Fri Feb 18, 2022 2:33 pm

B.Goode wrote:
Thu Feb 17, 2022 9:37 pm
BeJuice wrote:
Thu Feb 17, 2022 9:17 pm

I did create a folder called scripts.
... but that is not where you are trying to create or edit the python file.
Note that all file names and directories are case sensitive. "Scripts" is different from "scripts".

Also make sure that you are actually saving the file. The ".save" extension is an indication that the editor had an issue.

BeJuice
Posts: 7
Joined: Wed Feb 16, 2022 6:45 pm

Re: Python files not saving

Sun Feb 20, 2022 8:19 pm

Hi all, thanks for the replies.

I was able to fix this by using Thonny, instead of using nano in the terminal.

Return to “Beginners”