1

I cannot use subprocess since I need to run a windows desktop shortcut. This works good for me:

os.system("C:/Users/Administrator/Desktop/Neptune_Osprey_OCD_Daemon_xtensa_9.lnk")

I need to capture the output of the above desktop short which comes in command prompt. But I am unable to do it with os.system. Alternatively I tried with os.popen but still no success:

os.popen("C:/Users/Administrator/Desktop/Neptune_Osprey_OCD_Daemon_xtensa_9.lnk" > output.log 2 > OCD_Open_Log.txt)
user3565150
  • 884
  • 5
  • 21
  • 49
  • It should help http://stackoverflow.com/questions/12292594/writing-terminal-output-to-file – planet260 Mar 12 '15 at 13:36
  • why can't you use subprocess? – Padraic Cunningham Mar 12 '15 at 13:36
  • Since it is not an windows application, it is a desktop shortcut with proper attributes, therefore if I use subprocess it throws an error saying it is not a recognized windows application – user3565150 Mar 12 '15 at 13:39
  • 1
    `"some_string" > output.log 2 > OCD_Open_Log.txt` is not valid Python syntax. Did you mean for the rest of that line to be inside the string too? – Kevin Mar 12 '15 at 13:40
  • No, I just want to store the output in a text file when the shortcut is run – user3565150 Mar 12 '15 at 13:41
  • Low-tech solution: right click the shortcut and choose properties. copy the contents of the "Target:" text box, and use that in your `subprocess.check_output` call instead of the shortcut's path. – Kevin Mar 12 '15 at 13:46
  • @Kevin, in the 'Target' of the shortcut I get this xml file: ""C:\Program Files (x86)\Tensilica\Xtensa OCD Daemon 9.0.3\xt-ocd.exe" -config topology_all_cpus_Opsrey_Neptune.xml" – user3565150 Mar 12 '15 at 13:49
  • I can put this to os.system, but how do I capture the output in a file ? – user3565150 Mar 12 '15 at 13:49
  • By putting it in `subprocess.check_output` instead of `system`. Remember to supply a list of strings as the first argument and not a string. – Kevin Mar 12 '15 at 13:52

2 Answers2

0

This is a partial answer, just one step left:

 p = os.popen('"C:/Users/Administrator/Desktop/Neptune_Osprey_OCD_Daemon_xtensa_9.lnk" 2> OCD_Log.txt') 
while 1:
 line = p.readline()
 if not line: break
 print line 

This writes the output of the shortcut ran in command prompt to the OCD_Log.txt BUT only when I quit the command prompt by doing 'Ctrl + c'. And the OCD_Log.txt is also getting written with '^C' at the end of file.

The code is getting hanged because the application only quits when the command prompt is closed or Ctrl + c is done in the command prompt. Any suggestions to get over this last step ?

user3565150
  • 884
  • 5
  • 21
  • 49
-1

Use popen's resulting object to read its output:

import os

p = os.popen('ls .')
stdout = p.read()
p.close()

Moreover, you could open command for reading and for writing, passing data to command's stdin!

p = os.popen('command', 'rw')
p.write(my_input)
my_output = p.read()
p.close()

Yet, using popen from os module is deprecated. But, limited by your condition *do not use subprocess, that makes no difference.

shybovycha
  • 11,556
  • 6
  • 52
  • 82