7

I'm taking user input from the console but it will only accept 4096 bytes (4kb) of input. Since that is such a specific number is it something that is built into the language/is there a way around it?

The code I'm using:

message = input("Enter Message: ")
noviceOne
  • 75
  • 3

3 Answers3

3

is it something that is built into the language

No, the limitation is not part of Python, it's a limitation of the console shell.

is there a way around it?

That depends on your operating system. See this answer for how to enter more than 4096 characters at the console on Linux:

Linux terminal input: reading user input from terminal truncating lines at 4095 character limit

Community
  • 1
  • 1
samgak
  • 23,944
  • 4
  • 60
  • 82
1

4096 is 2^12

If you want larger input, please consider reading the message from a file instead.

with open('myfile.txt', 'r') as f:
    text = f.read()

Now, text will be a string which is all the text in the file. You can also do:

text = text.split('\n')

Now, text is a list of the lines in your text file

sshashank124
  • 31,495
  • 9
  • 67
  • 76
1

You need to disable canonical mode of TTY which can be done using termios. This will allow you to get lines loner than 4096 but you won't be able to edit lines using arrows, backspaces, etc.

import sys
import termios

def get_line(prompt=""):
  fd = sys.stdin.fileno()
  old = termios.tcgetattr(fd)
  new = termios.tcgetattr(fd)
  new[3] = new[3] & ~termios.ICANON
  try:
    termios.tcsetattr(fd, termios.TCSADRAIN, new)
    line = input(prompt)
  finally:
    termios.tcsetattr(fd, termios.TCSADRAIN, old)
  return line

str = get_line('test: ')
print('len: {}'.format(len(str)))
print(str)

It is the same as

stty -icanon
k3a
  • 1,296
  • 1
  • 15
  • 32