-2
import sys
from librk2.auth import Auth
from librk2.exceptions import LibrkError
from librk2.cr.server import CrServer
from librk2.helpers.ip4finder import IP4Finder

try:
    auth = Auth('my program', interactive=True)
    server = IP4Finder(auth, "1.1.1.1")
    deviceid = CrServer(auth, (server.device_id))
except LibrkError as ex:
    print(ex.message)
    sys.exit(ex.code)

sys.stdout=open("test.txt","w")
print("Current Machine type: {0}".format(deviceid.os_type))
sys.stdout.close()

After using sys.stdout=open and sys.stdout.close, the output is being written to only text file, but not coming on the terminal.

I want the output to be visible on the terminal as well as write to a text file. Consider it like a logging.

martineau
  • 119,623
  • 25
  • 170
  • 301
Navdeep.D2
  • 17
  • 2

2 Answers2

-1

All you have to do is put it all in two functions, like so

def outToTXT (text = ""):
    # magic

def log (text = ""):
    outToTXT (text)
    print (text)

print("What do you want to log?")
t = input()
log (t)

For any time you want to "also do something after running a method" just do what I did. I hope this is what you meant, and have a good day!

CiY3
  • 1
  • 3
-2

Use print and run

python myscript.py > text.txt
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
VoidNull
  • 9
  • 4
  • 2
    This only redirects the output to a file, but OP said they want the output _also_ on the terminal: "*I want the output to be visible on the terminal as well as write to a text file.*" – Gino Mempin Jun 18 '22 at 02:00