mpen
mpen

Reputation: 282805

How to use subprocess.call without shell=True

The documents recommend against using subprocess.call with shell=True, however when I try to do something as simple as

call(['convert'])

I get an error:

Must specify a file system

When I run the same command via cmd.exe, I get

C:\Users\Mark>convert
Version: ImageMagick 6.7.3-6 2011-11-10 Q16 http://www.imagemagick.org
...

etc., i.e., it actually runs.

So what's call doing? Why is it looking for a file?

Does it require a full path to the .exe? If so, I'd prefer not to do that, as I don't know where all the .exes are located.


Just tried it with the full file path to a program (call(['C:/imagemagick/convert.exe'])) and that works. But the question still stands...how do you do it without the full path? Just have it execute from working directory?

Upvotes: 2

Views: 1426

Answers (3)

user1556435
user1556435

Reputation: 1056

Coincidently I had this exact issue with the exact executable. This thread helped me point out the cause.

It seems to me that:

subprocess.call([shutil.which("convert")])

Is an easy and portable solution to this problem.

Upvotes: 1

SpliFF
SpliFF

Reputation: 38956

I believe dir is a builtin function of cmd.com, not a standalone program. You will need shell=True or a program that provides similar functionality to dir (like the ls.exe program in unixtools).

UPDATE FOR YOUR EDIT: What you're dealing with sounds exactly like a known issue/bug described here: http://bugs.python.org/issue8557 and here python subprocess Popen environment PATH?

It seems the behaviour of subprocess.call when shell=False is quite odd under win32. It seems like at a minimum you need to use convert.exe not convert and you need to search PATH yourself.

Upvotes: 4

lvc
lvc

Reputation: 35059

The issue here is better understood by trying dir.exe in a shell:

C:\Users\lvc>dir.exe
 Volume in drive C has no label.
 Volume Serial Number is 4B8C-511A

 Directory of C:\Users\lvc

File Not Found

This means that dir is not an executable anywhere in your %PATH% - rather, it is a command that the shell knows how to do without looking up a program for it. That means it will never, by definition, work with shell=False.

Upvotes: 2

Related Questions