6 Replies - 1247 Views - Last Post: 08 December 2011 - 07:21 AM Rate Topic: -----

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Bizarre os.system() result

Posted 07 December 2011 - 10:10 AM

Hi, I was under the impression that os.system("command") simulates the result when one enters command in the command prompt and hits the OK button. For example, if I run the command Excel from command prompt, a new Excel spreadsheet opens up. Naturally I was expecting that if I run os.system("Excel") from the interpreter, I will get the same result. However if I do so, all I get is a 1 typed into the interpreter (followed by the prompt in the next line), while a black window (looks like the command-line interpreter) appears and subsequently disappears within a fraction of a second. Why is this happening? How can I open a new Excel spreadsheet from Python interpreter?

Is This A Good Question/Topic? 0
  • +

Replies To: Bizarre os.system() result

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Bizarre os.system() result

Posted 07 December 2011 - 10:50 AM

I am just guessing here, but I would say the Excel executable you are calling actually spawns a new Excel process, immediately returning a result to os.system(). Because a value gets returns, os.system() doesn't block, and the Python script terminates. The operating system, detecting that the parent process has terminated, subsequently kills all child processes, namely your Excel process. The reason this doesn't happen when in a Python shell is that your interpreter is still open, meaning the OS doesn't kill your child processes until it terminates; to verify this, run your os.system() command in the interactive interpreter, and immediately after issue quit().

A work around is to have your script wait until the process itself dies; this can most easily be done with the subprocess module.
Was This Post Helpful? 3
  • +
  • -

#3 SegFaulty  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 11-October 10

Re: Bizarre os.system() result

Posted 07 December 2011 - 10:51 AM

On Windows, os.system() returns the exit status of the command that is given, so the reason you're getting that result is because for some reason or another Excel is exiting because of an error.

EDIT: Motoma beat me to it with a much better answer....

This post has been edited by SegFaulty: 07 December 2011 - 10:52 AM

Was This Post Helpful? 3
  • +
  • -

#4 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Bizarre os.system() result

Posted 07 December 2011 - 11:08 AM

View PostSegFaulty, on 07 December 2011 - 05:51 PM, said:

On Windows, os.system() returns the exit status of the command that is given, so the reason you're getting that result is because for some reason or another Excel is exiting because of an error.

EDIT: Motoma beat me to it with a much better answer....


Doesn't matter whether someone gave a better answer, thanks for trying to solve the issue...
Was This Post Helpful? 1
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Bizarre os.system() result

Posted 07 December 2011 - 09:07 PM

View PostMotoma, on 07 December 2011 - 05:50 PM, said:

The reason this doesn't happen when in a Python shell is that your interpreter is still open, meaning the OS doesn't kill your child processes until it terminates; to verify this, run your os.system() command in the interactive interpreter, and immediately after issue quit().


I guess you meant the os command line interpreter instead of Python shell/Python interpreter here? I didn't quite understand this part, how can I type quit? I click the Run button in the Start Menu, type Excel and then press OK. The moment I press OK, the window disappears so where do I type quit?

Another update, I was also under the impression that the Idle interpreter and the Python command-line interpreter does the same thing, except that the former is more user-friendly. However, when I run the command os.system("Excel") from the latter, I get 1 alright, but unlike the former, I also get an error message (see pic). Why this difference?

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by cupidvogel: 07 December 2011 - 09:19 PM

Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Bizarre os.system() result

Posted 08 December 2011 - 07:14 AM

It seems I misunderstood the problem. os.system() requires the full path to the excel executable:
import os
os.system("C:\\Program Files\\Microsoft Office\\Excel.exe")


Was This Post Helpful? 0
  • +
  • -

#7 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Bizarre os.system() result

Posted 08 December 2011 - 07:21 AM

Nah, same result as when I typed just Excel (and I think it should be so, cause os.sytem() just simulates whatever entering and running the same command from system command line would do. So if command-line doesn't require the full path, neither should os.system()). A 1 is typed into the interpreter, while a black command-line-interpreter appears on the screen and disappears almost in the wink of an eye! :helpsmilie:

This post has been edited by cupidvogel: 08 December 2011 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1