User avatar
Anne_Carlill
Posts: 72
Joined: Fri Jun 28, 2013 6:57 am
Location: York
Contact: Website Facebook Twitter

Python program not saved on R Pi2

Tue Sep 26, 2017 7:42 am

I feel really stupid here because the problem is so elementary. I started typing a Python game program into (Python3) Idle yesterday and named then saved it. As I typed I saved regularly and checked the file path so I knew where to find it again. When I came back to it later the icon for the file wasn't there and I couldn't retrieve my work. I opened Idle to look in Recent Files but it didn't show there either. I'm using the Pixel version of Jessie and I'd updated and upgraded earlier on to make sure I had pygame. Later I tried to download a couple of files from the Internet and when I came back to the Downloads folder they weren't there either. What am I doing wrong? Or is something amiss with my operating system?

SkyRise
Posts: 179
Joined: Tue Jan 24, 2012 1:20 pm

Re: Python program not saved on R Pi2

Tue Sep 26, 2017 8:01 am

This sounds like various threads on these forums.
You have a corrupted SD card (which happens for various reasons). This has been detected and to save whatever data which remains uncorrupted, the card has been set to Read Only.
Generally, your only recourse is a new SD card. You can still retrieve stuff you may need off the old one but it's no longer useable.

User avatar
Anne_Carlill
Posts: 72
Joined: Fri Jun 28, 2013 6:57 am
Location: York
Contact: Website Facebook Twitter

Re: Python program not saved on R Pi2

Tue Sep 26, 2017 8:09 am

Hi SkyRise,

Thanks for your quick answer! I suspected that would be the case. Right, I'll use a new SD card.
BTW, I looked for similar posts on Search but couldn't find anything relevant.

MarkDH102
Posts: 340
Joined: Fri Feb 13, 2015 3:18 pm

Re: Python program not saved on R Pi2

Tue Sep 26, 2017 2:06 pm

Ok, can I ask if someone knowledgeable would like to explain why the python editor, nano or any other app that is trying to save files to a "read only" SD card, does not display some kind of error to say that the file hasn't been written. Is it because to all intents and purposes once it issues the write command it says ok, I've stuck it all in the buffer ready to go to the SD card and that's all the write command knows so it just assumes it's written correctly?
It's just that us Pi owners seem to hit this problem regularly and it would be very useful if the OS (Jessie/Stretch) could pop up a message to say there was a problem saving because the SD card has gone read only? Then we could copy/paste said data into an email and save it that way to an external account/device thus not losing any data.
Cheers.

To the OP, how old is your SD card? Is it genuine (I know that's hard to tell). Did you install from NOOBS and then just start playing or has the Pi with that card been in use for some time?

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

Re: Python program not saved on R Pi2

Tue Sep 26, 2017 3:33 pm

MarkDH102 wrote:
Tue Sep 26, 2017 2:06 pm
Ok, can I ask if someone knowledgeable would like to explain why the python editor, nano or any other app that is trying to save files to a "read only" SD card, does not display some kind of error to say that the file hasn't been written. Is it because to all intents and purposes once it issues the write command it says ok, I've stuck it all in the buffer ready to go to the SD card and that's all the write command knows so it just assumes it's written correctly?
Yes. It is also possible that the card itself has reported no failure in order to prevent the OS from stalling.

MarkDH102
Posts: 340
Joined: Fri Feb 13, 2015 3:18 pm

Re: Python program not saved on R Pi2

Tue Sep 26, 2017 4:17 pm

So, is there a foolproof way for the OP and anyone else who's interested to check if a file is actually saved?
Can we quit the app, e.g Idle and then check for file in the file manager or will that report everything is OK until a reboot?
Bit of a nightmare really.
In my app, I guess I could flush the write queue and check if the file has got bigger?
Or is there a better way?
If the OS is writing data then surely every so often it should be able to check if it's gone to the SD card OK and provide an OS call to set and check status?

User avatar
DougieLawson
Posts: 35836
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Python program not saved on R Pi2

Tue Sep 26, 2017 4:38 pm

Foolproof in a LXTerminal.

cd /tmp
echo "Is this data saved" >> foo.txt
cat foo.txt



If it works you'll get

Code: Select all

[email protected] ~ $ cd /tmp
[email protected] /tmp $ echo "Is this data saved" >> foo.txt
[email protected] /tmp $ cat foo.txt
Is this data saved
[email protected] /tmp $
If it fails you'll get

Code: Select all

[email protected] ~ $ cd /tmp
[email protected] /tmp $ echo "Is this data saved" >> foo.txt
[email protected] /tmp $ cat foo.txt
cat: foo.txt: No such file or directory
[email protected] /tmp $
NOTE: You may need to do something to flush buffers to the SDCard, you may want to write a much bigger file (4K, 8K, or more) - make it something you can recognise or check with a sha1sum/sha256sum. You may want to write the file in /home/pi, shutdown the OS, reboot and then test it's still there in /home/pi.

You can also look at your /etc/fake-hwclock.data file, that gets an update at 17 minutes past every hour (from a file in /etc/cron.hourly). Is it monotonically increasing? Is it stuck near the time when your SDCard went bad?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Anne_Carlill
Posts: 72
Joined: Fri Jun 28, 2013 6:57 am
Location: York
Contact: Website Facebook Twitter

Re: Python program not saved on R Pi2

Wed Sep 27, 2017 7:07 pm

Thanks MarkDH102, for looking into this problem in more depth. And yes the SD card was an old one I bought with NOOBS already on it when I first started using an R Pi about 2 1/2 years ago. I've now bought a new one. I'm interested in this difficulty because I want to introduce using an R Pi in my Code Club but know that youngsters get very frustrated with problems like this so I need to know more before I can let the kids loose on it. Thanks for taking the debate further.

MarkDH102
Posts: 340
Joined: Fri Feb 13, 2015 3:18 pm

Re: Python program not saved on R Pi2

Wed Sep 27, 2017 7:43 pm

Well, I've got cameras and a video camera that have been using the same SD cards for many years without issue, so I can only assume that the OS is performing a very large number of writes in its normal operation.
So, you could maybe equip each Pi with a cheap USB stick that is only intended for saving students work. Again, I've used the same USB stick for many years with fairly heavy activity and as yet no issues.
Or have a central network drive off a 'normal' PC and make sure all work is saved there?
I don't know if Idle or Scratch can be set up to automatically save to one of these locations...

gkaiseril
Posts: 639
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: Python program not saved on R Pi2

Wed Sep 27, 2017 8:54 pm

Running into frustration is part of programming and life. As I learned in the service, ¨There are no obstacles only learning opportunities." Ans this is certainly a chance to for the students to learn how to deal with set backs by taking a deep breath and reevaluating the situation. and possibly looking for a different solution or learning more about the chosen approach.
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

User avatar
Anne_Carlill
Posts: 72
Joined: Fri Jun 28, 2013 6:57 am
Location: York
Contact: Website Facebook Twitter

Re: Python program not saved on R Pi2

Thu Sep 28, 2017 12:34 pm

Thanks MarkDH102 for some really useful ideas. I use a memory stick so I think it's worth using them with the pupils.

I would agree with you gkaiseril if I was their school teacher. However, the idea of Code Club is to get them interested and make it fun so I don't want to put them off coding early on.

User avatar
Anne_Carlill
Posts: 72
Joined: Fri Jun 28, 2013 6:57 am
Location: York
Contact: Website Facebook Twitter

Re: Python program not saved on R Pi2

Fri Sep 29, 2017 7:49 am

Thanks for your post DougieLawson. I've taken the time to read it properly now and will use your plan in the future - if I come across this problem again.

MarkyV
Posts: 97
Joined: Wed Sep 21, 2016 1:52 pm
Location: Buckingham, UK
Contact: Website

Re: Python program not saved on R Pi2

Fri Sep 29, 2017 9:27 am

I experienced this a couple of weeks ago and as a stop-gap to get the job done (a video shoot) I re-loaded the program from a usb drive after boot each time I powered up - a pain at the time but got the job done - this did get me thinking that if the OS does not complain it is quite a good system for workshops etc if the card is readonly but allows a temporary write and files could be saved to an external usb. Then after powerdown the whole thing is back to where it was before the workshop.

So my question is this: Is it possible to force an SD card into this state as it doesn't seem to have a write protect mechanism - and if so can it be toggled?

Thanks,

Mark
My RaspberryPi Blog: http://thepimaker.online

gkaiseril
Posts: 639
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: Python program not saved on R Pi2

Sat Sep 30, 2017 4:49 pm

Micro SD cards do not have a physical read/write tab. If one carefully checks many SD card manufacturers sites they will find that the manufacturer can and in many cases do add some proprietary code unique their product that can detect some failure conditions and locks the SD card so not further writes can occur in an attempt to save the data on the card from corruption. This allows for the maximum recovery of data. So it might be advisable to encourage all participants to have a spare SD card and backup their primary card on a regular basis. It is even possible that one may accidentally erase the entire SD card so a backup becomes very important.

.
Last edited by gkaiseril on Sun Oct 01, 2017 4:10 pm, edited 1 time in total.
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

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

Re: Python program not saved on R Pi2

Sat Sep 30, 2017 5:22 pm

gkaiseril wrote:
Sat Sep 30, 2017 4:49 pm
Micro SD cards to not have a physical read/write tab.
This is true. Full sized card do have a read/write tab, but that is simply a slider on the card that is detected by a switch in the socket. It doesn't do anything to the card itself. A lot of sockets don't have the detect switch, which means the read/write tab is ignored. The original Pi had the switch in the socket, but it wasn't connected to anything.

MarkyV
Posts: 97
Joined: Wed Sep 21, 2016 1:52 pm
Location: Buckingham, UK
Contact: Website

Re: Python program not saved on R Pi2

Sun Oct 01, 2017 12:56 pm

I'm still struggling to understand how this is working - with a corrupted card, the Pi seems to operate correctly - files can be saved and then reloaded (so I assume they are being written to the card) - but then on power off the new files are erased.

Is it that the corrupted area has been ring-fenced and the new files are written to a temporary area of the card?

So ... is it possible to configure a card with a read only area and a temporary area? That could be quite useful.

Thanks, Mark
My RaspberryPi Blog: http://thepimaker.online

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

Re: Python program not saved on R Pi2

Sun Oct 01, 2017 2:25 pm

MarkyV wrote:
Sun Oct 01, 2017 12:56 pm
I'm still struggling to understand how this is working - with a corrupted card, the Pi seems to operate correctly - files can be saved and then reloaded (so I assume they are being written to the card) - but then on power off the new files are erased.
The files are written to a buffer, that may be in the Pi, or on the card, but it is not permanent storage. The card will try to wait until it has a large amount of data ready to write as the write operation is slow and complicated (read old data into temporary storage, add new data, erase the old data block from Flash memory, write new datablock to Flash). The card may be holding that data in the temporary memory, but not writing it to the Flash. Also the Linux kernel will use most of the unused RAM as file buffers and read data back from there to speed things up.

gkaiseril
Posts: 639
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: Python program not saved on R Pi2

Sun Oct 01, 2017 4:13 pm

It should be noted that this is the way almost all removable data storage devices work on Pi, PCs, and Macs. That is why there is a required process to remove the attached removable device whether it is to unmounts the device, click a system try Icon to safely remove the USB device, or dragging the icon for the deice to the trash can.
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

Return to “Beginners”