0

I need to cat a file inside the OS module of python.

What I'm trying to do is what I have accomplished in Perl. Please note the cat /tmp/illinois.txt

my $cmd = `$ENV{BB} $ENV{BBDISP} "status $host.$test $COLOR \`date\` 
\`cat /tmp/illinois.txt\`"`;

I would like to do the same thing in python.

cmd = "{BB} {BBDISP} \"status {NODE}.{TEST} {COLOR} {NOW}\n {MSG}\n\"".format(
TEST = "gov-affairs-MemoryUtilization",
BB = os.environ["BB"],
BBDISP = os.environ["BBDISP"],
COLOR = COLOR,
NOW = datetime.datetime.now(),
MSG=`cat /tmp/illinois.txt\`,
NODE="xxxxx")
BioRod
  • 529
  • 1
  • 5
  • 19

1 Answers1

1

You can use the subprocess module to run commands from your script:

subprocess.run(cmd.split())

The run() function also takes parameters like shell=True if you want to keep the environment variables or stdout=subprocess.PIPE if you want to retreive the output of the command.

Honestly, just take a look at the docs ;)

EDIT: as Alan mentionned, the shell=True option is often considered not desirable, as you cannot be sure which shell is used by the system. More details here.

Fire
  • 393
  • 1
  • 7