-3

Possible Duplicate:
Executing command line programs from within python

How to execute single command within python and save everything between command and prompt to variable?
I've tried following code:

import os
var = os.system('ls')

Unfortunatelly, my var value is 0 instead 'ls' output.
How can I fix it?

Community
  • 1
  • 1
Djent
  • 2,877
  • 10
  • 41
  • 66

1 Answers1

1

You can use subprocess.check_output. Example from the linked documentation:

>>> subprocess.check_output(["echo", "Hello World!"])
'Hello World!\n'
mac
  • 42,153
  • 26
  • 121
  • 131