-6

Let's say I wrote the following python script:

print("Hello")
print("Line 2")
print("Goodbye")

The output would of course be:

Hello
Line 2
Goodbye

After printing this data is there any way I could read it? In other words, can you read printed data with python?

Binary111
  • 149
  • 4
  • 15
  • 3
    Read it from where? From the same script? From a different program running subsequently in the same terminal? From a program your first program was piped into? From the program that started the program that did the print? Something else? Without details about your specific use case, this is too broad to permit an answer. – Charles Duffy Jan 13 '18 at 00:09
  • 1
    (BTW, if you need some time to clarify and reformulate your question, temporarily deleting it yourself will prevent downvotes/close votes from taking place while you edit, and ensure that you can undelete the question on your own -- whereas if it's closed by vote, it can only be reopened via a vote). – Charles Duffy Jan 13 '18 at 00:11
  • The answer is yes, but sounds like an XY Problem. What do you want to do with that data? – OneCricketeer Jan 13 '18 at 00:43
  • @cricket_007, whether the answer is yes depends on the answers to the questions I asked in my first comment. If the OP wants to run one program with its output directly to the TTY, let it exit, and then run a second program, then it may very well be a "no" -- but at least it wouldn't be a duplicate if that were the case. – Charles Duffy Jan 13 '18 at 01:10

2 Answers2

1

You can use os.popen. First, create your script:

practice_script.py:

print("Hello")
print("Line 2")
print("Goodbye")

Then, in another script (or the interactive environment):

import os
print([i.strip('\n') for i in os.popen('python practice_script.py')])

Output:

['Hello', 'Line 2', 'Goodbye']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • @CharlesDuffy `import os;[i.strip('\n') for i in os.popen('python practice_script.py')]` can run outside of `practice_script.py`. – Ajax1234 Jan 13 '18 at 00:17
  • @CharlesDuffy my apologizes, I thought you were referring to the order of the code samples presented. In that case, you are correct, although I think the OP will have to clarify his intentions. – Ajax1234 Jan 13 '18 at 00:20
  • More to the point, though, we already have plenty of Q&A entries asking, and answering, how to do this -- meaning that **if** you're answering what the OP meant to ask, then the OP's question is a duplicate. – Charles Duffy Jan 13 '18 at 00:20
  • 1
    As an example, https://stackoverflow.com/questions/30664263/return-value-from-one-python-script-to-another, or https://stackoverflow.com/questions/6086047/get-output-of-python-script-from-within-python-script, or (if the pipe is set up externally) https://stackoverflow.com/questions/37847232/how-do-i-pipe-output-from-one-python-script-as-input-to-another-python-script – Charles Duffy Jan 13 '18 at 00:21
1
import subprocess

def system_call(command):
    p = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
    return p.stdout.read()

This function will take a command as input and can be used as follows:

output = str(system_call('python my_script.py'))

This can be used for mutli-line outputs and the entire output is stored in the string output

Jake
  • 617
  • 1
  • 6
  • 21