0

I'm writing a code to read serial input. Once the serial input has been read, I have to add a time stamp below it and then the output from a certain software. To get the output from the software, I want python to write a certain command to the terminal, and then read the output that comes on the terminal. Could you suggest how do I go about doing the last step: namely, writing to the terminal then reading the output? I'm a beginner in python, so please excuse me if this sounds trivial.

sixtyTonneAngel
  • 901
  • 2
  • 8
  • 11
  • 3
    Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – idjaw Aug 21 '16 at 13:37
  • @idjaw the OP is just asking how to read and write from the terminal. They aren't asking for someone to complete an application for them. – Robert Columbia Aug 21 '16 at 13:38
  • 1
    @RobertColumbia Feel free to answer this question if you think it is a good, complete question per SO rules. It is lacking a [MCVE] and it would help if the OP provided their own attempt at what they are trying to do, to even provide context to the readers to know what implementation approach they are taking to know where we can even begin to help. – idjaw Aug 21 '16 at 13:43
  • Do you mean this, OP? http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – Phu Ngo Aug 21 '16 at 14:05

2 Answers2

0

You would need to have python implemented into the software.

Also, I believe this is a task for GCSE Computing this year as I was privileged enough to choose what test we are doing and there was a question about serial numbers.

kallerdaller
  • 17
  • 1
  • 6
0

To run a command and get the returned output you can use the subprocess module's check_output function.

import subprocess

output = subprocess.check_output("ls -a", shell=True)

That will return the current directory contents in MacOS/Linux and store the output for you to read from later in your program. The "shell=True" allows you to execute a command as a string "ls -a". If you do not use "shell=True" you will pass the command as a list of each part of the command, example subprocess.check_output(["ls", "-a"]). Subprocess is a great module included with Python that allows a lot of command line execution.

So with subprocess you should be able to call another program, code, command, etc.by using a shell command.

John Morrison
  • 3,810
  • 1
  • 15
  • 15