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
  • 8,140
  • 8
  • 35
  • 49
Hacker
  • 133
  • 5
  • 16
  • 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
  • 278,196
  • 72
  • 453
  • 469
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,978
  • 1
  • 24
  • 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
  • 7,286
  • 6
  • 31
  • 45