0

I have a function as below which is working fine when I execute my python code in CMD (SIMT is an executable). However, when I built my executable with py2exe, a shell window quickly appear and disappear. So I searched and found out that I can use the subprocess.popen with creationflags= 0x08000000. But it does not work.

This is my function

def Kill(SIMT):
  outfile1 = open('Kill.txt', 'w')
  outfile1.write('Kill' + '\r\n')
  outfile1.write('x')
  outfile1.close()
  os.system("type Kill.txt | testclient p . " + SIMT)
  os.remove('Kill.txt')

and I replaced the os.system with:

  subprocess.Popen(["type Kill.txt | testclient p . ", SIMT], creationflags= 0x08000000, shell=True).communicate()

Also, do I need to have the shell=True?

Hamid K
  • 983
  • 1
  • 18
  • 40
  • 1
    what happens if you run `Popen(['testclient', 'p', '.', SIMT], stdin=PIPE, universal_newlines=True).communicate('Kill\nx')` instead? – jfs Mar 17 '15 at 19:20
  • Should I put Kill.txt instead of PIPE? `Popen(['testclient', 'p', '.', SIMT], stdin=Kill.txt, universal_newlines=True)` , what does the `.communicate('Kill\nx')` do? Should not I have the `creationflags= 0x08000000`? – Hamid K Mar 17 '15 at 19:43
  • do you see Kill.txt in my comment? – jfs Mar 17 '15 at 19:47
  • NameError: global name 'PIPE' is not defined. Oh you mean subprocess.PIPE – Hamid K Mar 17 '15 at 19:48
  • Do I need to have `creationflags= 0x08000000` in order to not see the sell be open and close quickly? Does it mean that I do not need the Kill.txt at all? – Hamid K Mar 17 '15 at 19:52
  • yes, both `Popen`, `PIPE` names are from `subprocess` module. Have you tried the code (no shell, no creationflags) from my comment? What is the result? Do you see the shell? – jfs Mar 17 '15 at 20:24
  • Yes, It worked fine with no shell, although I have the creationflags. Thanks Could you please help me to find the replaced command for this os.system("START " + S3_LOC + texe + " -p " + Floc[1] + " -f no -f no -f no -w " + S3_LOC), is this correct? subprocess.Popen(['START', S3_LOC , texe , '-p' , Floc[1], '-f no -f no -f no -w' , S3_LOC], stdin=subprocess.PIPE).communicate() – Hamid K Mar 17 '15 at 20:58
  • 1
    do not use `PIPE` and/or `communicate()` unless you want to provide input to the subprocess or get its output. `start` is an internal shell command. It requires shell. If `os.system('start ...')` does not work for you then ask a separate question: make sure to describe what do you expect to happen and what happens instead. Include the example input/output and the full traceback if any. – jfs Mar 18 '15 at 08:01

1 Answers1

1

If you want the shell to arrange the pipeline for you, you do need to have shell=True and have the first argument be a string, just like you had it for os.system, not a list. With shell=False, the list means to execute the program given as the list's first item, with the other items as command-line arguments to it; so you can't have the first item contain a | and expect the shell to arrange on your behalf that pipeline.

Your alternative is to arrange the "pipeline" or its equivalent yourself -- e.g, probably simplest here, just have a file object opened on Kill.txtas the standard input (stdin=) of the subprocess.Popen which only executes testclient (I believe type does nothing but read the file out to stdout, so that should suffice for this specific use case).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Should I write this way? `subprocess.Popen(['testclient', 'p', '.', SIMT], stdin=subprocess.PIPE, universal_newlines=True) .communicate('Kill\nx') Should not I have the creationflags= 0x08000000? This way I do not need to have the Kill.txt to be created. – Hamid K Mar 17 '15 at 20:22
  • Could you please help me to find the replaced command for this os.system("START " + S3_LOC + texe + " -p " + Floc[1] + " -f no -f no -f no -w " + S3_LOC), is this correct? subprocess.Popen(['START', S3_LOC , texe , '-p' , Floc[1], '-f no -f no -f no -w' , S3_LOC], stdin=subprocess.PIPE).communicate() – Hamid K Mar 17 '15 at 21:05
  • @user3261772, no it can't be correct, since, with no shell involved, nobody's going to "split on spaces" on your behalf, so at the end of the list you'll need `'-f', 'no', '-f', 'no', '-f', 'no', '-w, 'S3_LOC']`. – Alex Martelli Mar 17 '15 at 23:32