0

Here the trace

IOError: [Errno 2] No such file or directory: 'security01/30/15,15:32:58\n.jpg' 
#how and why \n come here?

for these line:

p = subprocess.Popen(['date +%m/%d/%y,%H:%M:%S']
stdout=subprocess.PIPE,shell=True)

Additional line used:

(output, err) = p.communicate()
    args = ['fswebcam','--no-banner','-r',' 960x720','filename' +       str(output) + '.jpg']
    subprocess.call(args)

Another line:

mail('mail@mail.com',
   'subject',
   'body',
   'filename' + str(output)  + '.jpg')
moz ado
  • 490
  • 4
  • 17

2 Answers2

2

date returns with a newline at the end of its output, so you need the strip() method to get rid of it.

p = subprocess.Popen(['date +%m/%d/%y,%H:%M:%S'], stdout=subprocess.PIPE, shell=True)
stdout = p.communicate()[0].strip()
print 'security{date}.jpg'.format(date=stdout)
>>> security01/30/15,10:36:24.jpg

If you're looking to add a date/timestamp to your filename though, you would be better off doing it directly in Python using the datetime module and the strftime function:

from datetime import datetime
print datetime.now().strftime('%m/%d/%y,%H:%M:%S')
>>> '01/30/15,10:47:37'
ncocacola
  • 485
  • 3
  • 9
  • was able to remove the \n with strip() but got another error.Error opening file for output: filename01/30/15,16:10:32.jpg, and thanks for the help. – moz ado Jan 30 '15 at 10:45
  • You cannot have slashes in your file names unless you have directories in place, so `filename01` containing a subdirectory `30` in which you want to create a file named`15,15:10:32.jpg`. – tripleee Jan 30 '15 at 10:58
0

The date command, and basically every other command intended for interactive command-line use, terminates its output with a newline.

If that is not what you need, trimming the final newline from the output of a subprocess call is a very common thing to do (and in the shell, the hardcoded default behavior of process substitutions with `backticks` and the modern $(command) syntax).

But you don't need a subprocess to create a date string -- Python has extensive (albeit slightly clunky) support for this in its standard library, out of the box. See e.g. here.

import time
filename = time.strftime('security%Y.%m.%d_%H.%M.%S.jpg')

or, adapted into your first example snippet,

args = ['fswebcam','--no-banner','-r',' 960x720',
    time.strftime('filename%Y.%m.%d_%H.%M.%S.jpg')]

Because both slashes and (to a lesser extent, but still) colons are problematic characters to have in file names, I have replaced them with dots. For purely aesthetic reasons, I also changed the comma to an underscore (doubtful; underscores are ugly, too).

I also switched the generated file names to use a standard datestamp naming convention with a full-digit year first, so that file listings and glob loops produce the files in correct date order.

Probably the code should be further tweaked to contain a proper ISO 8601 date in the file name; then if you want to parse and reformat it for human consumption separately, you are free to do that. But avoid custom date formats when there are standard formats which can be both read and written by existing code, as well as unambiguously understood by humans.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • thank you for the help, the above solved the problem of new line, but fswebcam is not able to write the image on local disk/sdcard. so the mail attachment give error. – moz ado Jan 30 '15 at 11:08
  • I don't think that other problem can be solved in the context of this question. Feel free to post a new question, probably with a new and focused description of your new problem (but feel free to link to this question for background) and perhaps mark this answer as accepted. – tripleee Jan 30 '15 at 11:09
  • Got it working, was the file name. Thanks for the help – moz ado Jan 30 '15 at 11:13