0

I have a question regarding the python programming, let's say i have a loop, so within the loop, i want to stdout.write 3 variables in different lines. For example:

while (True):
    a += 0
    b += 5
    c += 10
    sys.stdout.write("\routput1 = %d" % a)
    sys.stdout.write("\routput2 = %d" % b)
    sys.stdout.write("\routput3 = %d" % c)

So in the terminal should be like:

output1 = .........
output2 = .........
output3 = .........

Each output just remain in their lines and keep refreshing. Thank you!

sunboy612
  • 13
  • 4

2 Answers2

0

For single line on Windows you can do a sys.stdout.write() and then write cursor movements '\b' to move the cursor back to the beginning of the line.

Example of a copy file function with % progress indicator on the same line, using this method:

import os
import sys

def copy_progress(source_file, dest):
    source_size = os.stat(source_file).st_size
    copied = 0
    source = open(source_file, 'rb')
    target = open(dest, 'wb')
    print ('Copy Source: ' + source_file)
    print ('Copy Target: ' + dest)
    print ('Progress:')
    while True:
        chunk = source.read(512)
        if not chunk:
            break
        target.write(chunk)
        copied += len(chunk)
        progress = round(copied * 100 / source_size)
        my_progress = str(progress).ljust(5)
        sys.stdout.write (my_progress + '%\b\b\b\b\b\b')
        sys.stdout.flush()
    sys.stdout.flush()
    source.close()      
    target.close()  
Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
0

For multiple line printing, each console window on Windows is a number of lines, with number of columns... usually 25 lines and 80 columns as an old standard.

You can move a cursor to an (y, x) position, and print a string on screen.

y = line x = column

Example code:

import ctypes
from ctypes import c_long, c_wchar_p, c_ulong, c_void_p

handle = ctypes.windll.kernel32.GetStdHandle(c_long(-11))

def move_console_cursor(y, x):
    value = (x + (y << 16))
    ctypes.windll.kernel32.SetConsoleCursorPosition(handle, c_ulong(value)) 

def print_string_at_cursor(string):
    ctypes.windll.kernel32.WriteConsoleW (handle, c_wchar_p(string), c_ulong(len(string)), c_void_p(), None) 

You then can print multiple lines over eachother by moving the cursor to the appropriate position and then print a string with the functions given.

A 3 line example: Easy would be to do a os.system('CLS') to clear the screen, and then you can move cursor to 1,1 2,1 3,1 and repeat until you have done all your processing. At the end, don't forget to move your cursor to 4,1. You can of course pick any location of your console window.

Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
  • Thank you for the answer, but like I am using for example git bash or similar terminal, not windows command window, so is there a way to solve this for linux like console? – sunboy612 Feb 02 '18 at 10:10
  • @sunboy612 you did not specify, and when asked you replied Win10. The answers you are looking for are already mentioned in the comments section. – Edwin van Mierlo Feb 02 '18 at 13:11
  • Yes! Thanks a lot for the help! – sunboy612 Feb 06 '18 at 07:47