0

So I got a Raspberry pi and I've been playing around with it for a couple days now. I'm making a "camera" program where you press a button on a breadboard, it takes a picture using fswebcam, and it saves it to a flash drive. I am on Raspbian. This is my code:

import os
import random
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
#I know this next part looks a little weird, but in Raspbian every time you take out a flash drive it keeps a folder, then when you put the drive back in it calls it a slightly renamed version which messes up my code, and this next part will erase all the weird folders that have no use so that the flash drive will still be called FLASHDRIVE.
os.system("cd /")
os.system("cd /media")
#I hope the previous line worked,
os.system("sudo rm -rf *")
#The following function will wait until the buton is pressed:
def waitforbutton():
    if(GPIO.input(2) == False):
        return True
temp = waitforButton()
#The program will save the image as a random number.jpg
imgname = str(random.random) + ".jpg"
os.system("fswebcam /media/FLASHDRIVE/" + imgname)
print("Picture taken")

But, when I type:

sudo python camera.py

into the terminal, it prints a GPIO warning message and then this:

sh: 1: cannot open built-in: No such file
picture taken

And then if I run the program again, it says:

python: can't open file 'camera.py': [Errno 2] No such file or directory

Please help!!!!!

  • 1
    I suspect you deleted everything in the directory where you executed your python script. I don't think os.system("cd /media") works the way you expect it to. – Joe Young Aug 27 '15 at 16:11
  • 1
    http://stackoverflow.com/questions/431684/how-do-i-cd-in-python – Joe Young Aug 27 '15 at 16:12
  • 1
    You can also use os.chdir to change directories, also `random.random` is almost certainly not what you want, you never call it, so you are basically creating a file name like `'.jpg'`, random.random when used correctly returns a float, I imagine you want to use `random.randint(start, end)` – Padraic Cunningham Aug 27 '15 at 16:24
  • 1
    You second error is because you have deleted the .py file,why are you calling `rm -rf *`? – Padraic Cunningham Aug 27 '15 at 16:30

2 Answers2

0

Each call to os.system starts a new child process. Environment changes made in a child process don't propagate to the original parent process. The cd that you do in one call does not set the directory that you'll be in when you make the next call. So when you then do sudo rm -rf *, you're still in your original directory, not /media, and you remove everything there (including the camera.py script that you were executing).

You can call os.chdir(), which changes the directory of the python process itself, and this will be inherited by the children.

os.chdir('/media');
os.system('sudo rm -rf *')

You should also add error checking to the os.chdir() call, because sudo rm -rf * is a very destructive if you're not in the directory you expect. If you do that from a system directory you could totally cripple the system.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks! That deletes the media folder except for Settings and FLASHDRIVE, but I'm still getting that weird error message and it doesn't save the picture. –  Aug 27 '15 at 16:20
  • Do you know which line in the script is causing that error? – Barmar Aug 27 '15 at 16:21
  • So find out. Add some `print` statements to the script, and narrow down where the error occurs. – Barmar Aug 27 '15 at 16:26
  • 1
    The weird name comes from `str(random.random) + ".jpg"`, random is never called – Padraic Cunningham Aug 27 '15 at 16:29
  • I think it can't find the flash drive in the media folder. –  Aug 27 '15 at 16:30
  • 1
    I fixed it! Thanks, Padraic Cunningham. I forgot to add the () after random.random. –  Aug 27 '15 at 16:32
0

In the first place, never ever use sudo rm -rf * it's too dangerous. And it's also reason why the next time you tried to run camera.py it failed because you already deleted it.

Call to os.system create sub process where it run passed command. That means that os.system('cd /media') will change current working directory for the sub process but not for the process of your python script.

To change working directory of your python process use os.chdir.

By the way your function waitforbutton will actually not wait for the button to be pressed. You need to put that check in a loop, or maybe even better use GPIO's interrupts for this.

beezz
  • 2,398
  • 19
  • 15