3
import sys
import os

log = os.system('cat /var/log/demesg')

This code prints the file by running the shell script cat /var/log/dmesg. However, it is not copied to the log. I want to use this data somewhere else or just print the data like print log.

How can I implement this?

APerson
  • 7,746
  • 8
  • 34
  • 48
Hacker
  • 133
  • 5
  • 15
  • See for example this thread of how to get output from shell commands: http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output – Rolle Feb 04 '13 at 12:59

3 Answers3

9

Simply read from the file yourself:

with open('/var/log/dmesg') as logf:
    log = logf.read()
print(log)
phihag
  • 263,143
  • 67
  • 432
  • 458
2

As an option, take a look at IPython. Interactive Python brings a lot of ease of use tools to the table.

ipy$ log = !dmesg
ipy$ type(log)
 <3> IPython.utils.text.SList
ipy$ len(log)
 <4> 314

calls the system, and captures stdout to the variable as a String List.

Made for collaborative science, handy for general purpose Python coding too. The web based collaborative Notebook (with interactive graphing, akin to Sage notebooks) is a sweet bonus feature as well, along with the ubiquitous support for parallel computing.

http://ipython.org

Brian Tiffin
  • 3,918
  • 1
  • 23
  • 34
1

To read input from a child process you can either use fork(), pipe() and exec() from the os module; or use the subprocess module

LtWorf
  • 6,904
  • 5
  • 30
  • 41