简体   繁体   中英

python in applescript: subprocess.call vs os.system in automator

I found a work around when calling a python script from a shell script from automator (version 2.1.1 in mac os x 10.6.8) (python 2.6.1). My issue is that os.system("mkdir foo") works in a (very specific) case where subprocess.call("mkdir foo") also makes the directory but then makes automator throw an error.

The purpose of my automator app is to accept an image that is dragged-and-dropped onto it. The automator app has just one action: 'run shell script' with the following code:

for f in "$@"
do
    python dphoto_pythons/uploader.py $f > dphoto_pythons/auto_output.txt
done

$f is the name of the dragged image. ('Run shell script' is set to pass input as arguments.) Here is the strange part:

in the 'uploader.py' script, I was calling this:

retcode=call("mkdir " + dir_full, shell=True) # THIS CAUSES A ERROR IN AUTOMATOR
print "***DOESN'T GET HERE WHEN RUN IN AUTOMATOR****"

It makes the directory. but doesn't get to the next statement. And automator would throw up an error dialog (which just says that I should go check my script).

By contrast, if I called uploader.py 'manually' from the terminal:

"python uploader.py someimage.jpg"

it worked without a hitch. After puzzling over this for a bit, I tried replacing call with os.system:

os.system("mkdir " + stu_dir_full)
####retcode=call("mkdir " + stu_dir_full, shell=True) #  BUG

This works from automator and from terminal. I have a feeling that I'm over-looking something obvious because this is such a bizarre problem. But thought I'd put this out there in any case. Any thoughts?

-Mel

You could use os.mkdir(stu_dir_full)...

But the problem with your subprocess.call line might be that subprocess.call is expecting a list not a string. Try this:

retcode = call(["mkdir",dir_full], shell=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM