-2

I'm programmatically running scripts from another process. And, waiting for outputs, on the stdout stream, from within my own functions.

But, as python's (and javascript's) console does, it automatically prints the result of my expression to the console (thus the stdout stream).

Can I stop this, so that I can execute expressions and use the stdout stream to solely read my output?

Tobiq
  • 2,489
  • 19
  • 38
  • 2
    what console ? Where do you run it? Normally it doesn't print if you don't use `print()`. Do you have to run it in this console? Can't you run it normally in normal terminal? – furas Feb 11 '20 at 23:57
  • @furas I don't get your questions. I've ran `python` and I'm now inputting something like `2*2\n`, and `4` is outputted – Tobiq Feb 12 '20 at 00:03
  • 2
    That's not `print()`ing, it's the expression's return value. It's very useful when interactively running code. What workflow are you using where this would matter? I'm confused by "I'm programmatically running scripts" and your assertion that you're seeing return values. The more information we have the more likely we'll be able to make useful suggestions. – ChrisGPT was on strike Feb 12 '20 at 00:12
  • 3
    normally we run as `python script.py` and we have to use `print()` to display it. If you run it in different way then describe it. – furas Feb 12 '20 at 00:19
  • My question is clear, I want to turn the `very useful` feature off. Is it possible or not. – Tobiq Feb 12 '20 at 00:23
  • 2
    Tobiq, it is _not_ clear. "I'm programmatically running scripts from another process" conflicts with the rest of what you're telling us. – ChrisGPT was on strike Feb 12 '20 at 00:31
  • I'm running the python terminal. I want to stop the automatic printing of the expression evaluations. – Tobiq Feb 12 '20 at 00:35
  • The reason I don't want this printing is because I'm using the stdout for my own (binary) output. – Tobiq Feb 12 '20 at 00:36
  • @HeapOverflow What is that? – Tobiq Feb 12 '20 at 00:37
  • 1
    @Tobiq, what does this have to do with stdout? Please read [ask]. It is your responsibility to ask as clear a question as you can. We can't help you if you don't. – ChrisGPT was on strike Feb 12 '20 at 00:38
  • You can't help because you don't even understand what I'm talking about... The printing is **outputted to the terminal (the stdout)** – Tobiq Feb 12 '20 at 00:38
  • @HeapOverflow Any explanation on how it works? – Tobiq Feb 12 '20 at 00:40
  • @HeapOverflow on stack overflow, you don't post random code snippets with literally no explanation of how it works / where it's from... – Tobiq Feb 12 '20 at 00:44
  • I had already searched google for the snippet, to find no explanation of what it does (because it's a random code snippet) – Tobiq Feb 12 '20 at 00:46
  • Then surely you can provide some explanation of what its doing? – Tobiq Feb 12 '20 at 00:46
  • yes, why are you setting the function to `str`. That's not an efficient no-op – Tobiq Feb 12 '20 at 00:48
  • 1
    _The printing is outputted to the terminal (the stdout)_ Are you just asking how to redirect stdout? This practically seems like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. Unfortunately, I still don't understand the situation you're describing, and the fact that you're _programmatically running scripts from another process_ makes it even more confusing. – AMC Feb 12 '20 at 01:47
  • @AMC Its not... – Tobiq Feb 12 '20 at 10:16

2 Answers2

1

you can redirect the standard output

>>> import os
>>> import sys

>>> real_stdout = sys.stdout # saving real stdout
>>> sys.stdout = open(os.devnull, 'w') # redirect to devnull

>>> 3 * 4  # no console output
>>> print('hello world', file=real_stdout) # if you want to print something just use the real stdout
hello world

>>> sys.stdout.close()  # at the end reverse the changes
>>> sys.stdout = real_stdout
>>> 3 * 4 # now there is the console output
12
Hoxha Alban
  • 1,042
  • 1
  • 8
  • 12
-1

So working with what I read in the comments on your question I think this is what you're looking for.

>>> 2*2/8
0.5
>>> x = 2*2/8
>>> 

From what I've gathered you want to disable return values in the interactive interpreter, which, as far as I know, isn't possible.

You could also take a look at this answer.

Jeroen
  • 279
  • 1
  • 6
  • The reason I don't want this printing is because I'm using the stdout for my own (binary) output. – Tobiq Feb 12 '20 at 00:36