I seem to be having trouble with my Raspberry Pi as it won't let me write files on an inserted SD card. I am trying to create a SD card eraser (When a card is inserted, it erases). I have tested many times, yet it always stops on the same device, and displays this:
Running at boot
['pi']
OK--for i in listOfInsertedDevices:
pi
OK--for x in listOfInsertedDevices:
SD_CARD
pi/SD_CARD
Traceback (most recent call last):
File "/home/pi/Program.py", line 29, in <module>
shutil.rmtree("/media/"+i+"/"+x)
File "/usr/lib/python2.7/shutil.py", line 256, in rmtree
onerror(os.rmdir, path, sys.exc_info())
File "/usr/lib/python2.7/shutil.py", line 254, in rmtree
os.rmdir(path)
OSError: [Errno 16] Device or resource busy: '/media/pi/SD_CARD'
My python code "/home/pi/Program.py":
import os #!! CRITICAL !!# # This gets the name of the files, and
helps sorting.
import time # Time #
import shutil # This removes entire directories, no matter what they
contain.
from subprocess import call
call(['espeak "Python file Program.py at location forward slash home
forward slash pi is now online" 2>/dev/null'],shell=True)
while True: #Run on a 2 second loop
listOfInsertedDevices = os.listdir("/media") #Get the devices in the
media directory
print(listOfInsertedDevices) #Debug
number = 0 #This is helpful
for i in listOfInsertedDevices: #Get a device
print("OK--for i in listOfInsertedDevices:")
print(i) #Debug
del listOfInsertedDevices[number] #Remove it from the list
for x in os.listdir("/media/"+i): #Find files in device
print("OK--for x in listOfInsertedDevices:")
print(x)
print(i+"/"+x)
if (i+"/"+x) != ("pi/SETTINGS1"):
shutil.rmtree("/media/"+i+"/"+x)
else:
call(['espeak "Sorry, file is not to be accessed." 2>/dev/null'],shell=True)
number += 1
time.sleep(2)
Also, I set this code to run on boot in my .bashrc file.
Can anyone help?
Thank you.
~/.bashrc
does not run at boot time. It is executed bybash
for non-login shells. See/usr/share/doc/bash/examples/startup-files
(in the package bash-doc) for examples.