2

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?

mpen
  • 237,624
  • 230
  • 766
  • 1,119
  • 1
    Not an expert at Windows, but one guess is try 'dir.exe' – R Samuel Klatchko May 31 '12 at 02:22
  • @RSamuelKlatchko: Same error. File extension can usually be omitted for .exes on windows. – mpen May 31 '12 at 02:23
  • You're using a Python interpreter installed on Windows, not a Python interpreter inside Cygwin or a similar environment, right? – gotgenes May 31 '12 at 02:58
  • @gotgenes: \*shifty eyes\* Was running it through my IDE. ... Nah, just tried through the cmd shell (`> python calltest.py`), and it does the exact same thing as when I run it through the IDE, which just runs normal Python (not Cygwin). – mpen May 31 '12 at 03:02
  • @RSamuelKlatchko `dir` is a shell built-in on Windows, not a program. – Joey May 31 '12 at 07:21

2 Answers2

4

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.

Community
  • 1
  • 1
SpliFF
  • 35,724
  • 15
  • 80
  • 113
  • Ah! Okay. That was a bad example then. I actually want to use ImageMagick's `convert`, which is an *actual* exe. Having the same problem there. – mpen May 31 '12 at 02:50
  • re:your update: That bug sounds exactly like the issue I'm having. Nice to see the second I dive back into Python I get slapped with a library bug! Haha! Thanks. – mpen May 31 '12 at 07:16
2

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.

lvc
  • 30,969
  • 8
  • 62
  • 94
  • Interesting. Never tried running it with the .exe before. If I had edited my environment variables to include whatever directory dir.exe is in, it would work? – mpen May 31 '12 at 02:26
  • @Mark you'll find that including `dir`s directory in `%PATH%` might be difficult, since the issue isn't that it isn't there already with the shell doing magic to be able to find it anyway. The issue is that `dir` isn't implemented by a standalone `.exe` - there *is no* `dir.exe` to include. – lvc May 31 '12 at 02:32
  • @lvc: Oooh. Okay. I was just using `dir.exe` as a [bad] example anyway.. I'm really using ImageMagick's convert, which *is* somewhere on my path. Running that via python (`call(['convert'])`) says "Must specify a file system", but via cmd actually runs the program. – mpen May 31 '12 at 02:49
  • @Mark I can't reproduce this with `convert` - it runs fine, and exits with `4`. You may wish to check `os.environ['PATH']` to make sure the path python is seeing agrees with the one cmd is seeing. – lvc May 31 '12 at 03:10
  • Is the command `dir.exe` being interpreted as `dir .exe` or something (so that the `dir` command shows `File Not Found` due to the absence of a `.exe` file)? Otherwise I'd expect to see `'dir.exe' is not recognized as an internal or external command, operable program or batch file.`. – Karl Knechtel May 31 '12 at 03:53
  • @KarlKnechtel I believe so, yes. Someone else had previously commented that that was the case, but that appears to have been deleted. – lvc May 31 '12 at 04:40