0

I have a python script to send me an email when i detect motion from my noir Pi camera, which is been successfully called.

I know the script is being called, as i get the email on movement; but the script doesn't copy the jpgs from /mnt/ to /home/pi/box/pi_pictures

Have i got the subprocess.call syntax correct at the bottom of this script?

#!/usr/bin/env python

import smtplib
import subprocess


from email.mime.text import MIMEText

USERNAME = "theemailaddressisendfrom.com"
PASSWORD = "mypassword"
MAILTO  = "myemailaddress.com"

msg = MIMEText('blar blar')
msg['Subject'] = 'from pi script test1'
msg['From'] = USERNAME
msg['To'] = MAILTO

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()


time.sleep(20)
subprocess.call("cp /mnt/*.jpg  /home/pi/box/pi_pictures", shell=True)
  • When you are looking into the the /mnt directory can you see you jpg files doing a ls command or are they in a sub directory ? – dry Mar 4 '14 at 14:45
  • I can see them in the /mnt directory( no sub directory). the command "cp /mnt/*.jpg /home/pi/box/pi_pictures" works from the command line, but not from the python script. – reggie Mar 4 '14 at 15:10
  • If you are seeing them then maybe try to catch the output of the command execution calling subprocess_check_output function (if you are using python version > 2.7 ) subprocess.check_output(['cp', '/mnt/*.jpg','/home/pi/box/pi_pictures']) – dry Mar 4 '14 at 15:30
  • subprocess.check_output(['cp', '/mnt/*.jpg','/home/pi/box/pi_pictures']) doesn't work either – reggie Mar 4 '14 at 16:23
  • I always use subprocess.call("cp /mnt/*.jpg /home/pi/box/pi_pictures", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). Maybe this helps. Otherwise try adding the full path of cp, So "/bin/cp /mnt..." – Gerben Mar 4 '14 at 16:45
1

Iv'e just found out what was wrong,

the script was hanging at time.sleep(20) because i didn't have import time at the top.

Oops!

0

I suspect it's the wildcard *.jpg in there messing it all up. If you're using that you have to run it with the shell=True. At that point the first argument must be a single line, like you have it. Breaking it down into an array will result in the * being treated as a literal and the different file names won't be inserted instead. You can try adding

stdout=subprocess.PIPE, stderr=subprocess.PIPE 

to get some output from the command.

If you don't specifically need the capability of subprocess, it may be worth trying to use

os.system("cp /mnt/*.jpg  /home/pi/box/pi_pictures") 

instead.

  • I tried adding 1) import os at top and 2) os.system("cp /mnt/*.jpg /home/pi/box/pi_pictures") at end, but still didn't work. – reggie Mar 4 '14 at 17:25
  • the cp = coppy, is not part of the path, i assumed people knew that – reggie Mar 4 '14 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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