0

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.

1
  • scripts in ~/.bashrc does not run at boot time. It is executed by bash for non-login shells. See /usr/share/doc/bash/examples/startup-files (in the package bash-doc) for examples.
    – Ingo
    May 9, 2018 at 16:43

1 Answer 1

0

I saw the documentation of the function shutil.rmtree(path), if you read here in this link how works the function, this function delete all the directory, in your case you try to delete the directory of the SD Card when this SD Card is mounted, that's why there is an error. So if the idea is just delete the content of the SD Card you can use another way for example using the OS Library and use the method rm /media/pi/SD_CARD/*, this line remove all the files inside of the directory. You can try this without code by yourself in a terminal and then program the code in Python to do exactly what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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