[Answer]-Trying to Execute Python Script Using Subprocess (Django)

1👍

Converting a list to a shell string using ‘ ‘.join(…) is risky because there may be something in the list (like a space in a file name) that needs shell escaping. You are better off just sticking with the command list and not the shell. You should also capture stderr which is where the good stuff is going to be. Finally use check_call and wrap the whole thing in an exception handler that logs execution failures.

try:
    commandString = [
        'python',
        os.path.join(SCRIPT_DIR, 'ffmpeg.py'),
        '-i', os.path.join('/srv/nfsshare/transcode50', userFolder, directory, title),
        '-d', os.path.join('/srv/nfsshare/transcode50', userFolder, directory),
        '-r', request.POST['framerate'],
        '-p 2', '-f', ",".join(formats), '-t', ",".join(rasters)
    ]

    # call transcode50 script to generate condor_execute.py
    subprocess.check_call(commandString, 
        stdout=open('/srv/nfsshare/transcode50/output.txt', 'w'),
        stderr=subprocess.STDOUT)

except Exception, e:
    # you can do fancier logging, but this is quick
    open('/tmp/test_exception.txt', 'w').write(str(e))
    raise

Leave a comment