0

I'm using this code to start the batch file but

import os
os.startfile("C:\Documents and Settings\Zha\Desktop\Strings\strings.exe ")

the strings.exe running a batch command when you open it from cmd, like this:

strings -q "C:\Documents and Settings\Zha\Desktop\folder\*"

and I need to run it from python. Possible way to solve this?

1 Answers1

0
from subprocess import Popen, PIPE

handle = Popen(['strings', '-q', 'C:\\Documents and Settings\\Zha\\Desktop\\folder\\*'], stdout=PIPE)

while handle.poll() is None:
    print(handle.stdout.readline())
handle.stdout.close()
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 1
    `shell=True` could be a risk! – ρss Jun 10 '14 at 12:47
  • Does the normally-equivalent `Popen(['strings', '-q', 'C:\...\*'], stdout=PIPE)` work? If so, that's a better answer. – Veedrac Jun 10 '14 at 12:59
  • @pss It's not actually likely to be a risk as a *constant* is being passed to `Popen`, but it's a lot like `eval`: *don't* use it unless you *have* to. And you *never* have to (with the exception of giving an interface directly to the shell, which is rare to say the least). – Veedrac Jun 10 '14 at 13:01
  • Will correct it in a bit. – Torxed Jun 10 '14 at 14:05
  • I omit 'shell=True' and it worked – TheGameDoctor Jun 10 '14 at 15:58
  • @TheGameDoctor It works both with and without, it's just a matter of how to input the command. `shell=True` can take a single string while alse` needs a list if i'm not mistaken. Also the eval thing. – Torxed Jun 11 '14 at 07:38