Set Console Width (Windows, Python)

18,425

Solution 1

The easiest way is to execute the mode command.

e.g. for a 80x25 window:

C:\> mode con: cols=25 lines=80

Or in Python:

subprocess.Popen(["mode", "con:", "cols=25", "lines=80"])

Solution 2

a) To check the size of the Terminal Window

import os

x = os.get_terminal_size().lines
y = os.get_terminal_size().columns

print(x)
print(y)

b) To change the size of the Terminal Window

import os

cmd = 'mode 50,20'
os.system(cmd)

c) To change the color of the Terminal Window

import os

cmd = 'color 5E'     
os.system(cmd)
Share:
18,425
Fourier
Author by

Fourier

Physics Student from Germany.

Updated on June 09, 2022

Comments

  • Fourier
    Fourier almost 2 years

    I am working on a little text based console game using Python. Because I am using some ASCII art I have to ensure that the width of the console is the same for everyone after launch of my game. Can anyone tell me how to set the console width and height ? :)

    Greetz

    Flo

  • Fourier
    Fourier over 11 years
    Ok I imported subprocess but it still does not work :c i get a "FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden" error -.-
  • Fourier
    Fourier over 11 years
    Ok got it working by myself ;) I just used os.system("mode con: cols=25 lines=80")
  • Meow
    Meow almost 8 years
    LOL, mode is a cmd command, not a executable, subprocess.Popen(["mode", "con:", "cols=25", "lines=80"]) definitely does not work.
  • aschultz
    aschultz over 5 years
    This answered my next question! A follow-up is that it is for 3.3 or higher, so Python 2 users need to import future.
  • Daniel
    Daniel almost 4 years
    How can I reset the size?
  • Levon
    Levon almost 3 years
    This is super - but I guess as part of the functionality of the mode command the scroll bar disappears :-/
  • knw
    knw about 2 years
    Not system independent though, Windows only, for MacOS see: stackoverflow.com/questions/59356799/…