-5

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?

4
  • 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. Commented Jan 13, 2018 at 0: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). Commented Jan 13, 2018 at 0:11
  • The answer is yes, but sounds like an XY Problem. What do you want to do with that data? Commented Jan 13, 2018 at 0: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. Commented Jan 13, 2018 at 1:10

2 Answers 2

2
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

Sign up to request clarification or add additional context in comments.

Comments

2

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']

4 Comments

@CharlesDuffy import os;[i.strip('\n') for i in os.popen('python practice_script.py')] can run outside of practice_script.py.
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.