5

Possible Duplicate:
Setting smaller buffer size for sys.stdin?

I have an input line that is longer than 4096 bytes coming into stdin in Python. The code simply says:

line = sys.stdin.readline()

which is truncating line at 4096 bytes. Anyone know a solution?

Community
  • 1
  • 1
zsimpson
  • 765
  • 2
  • 9
  • 13
  • Works fine here (Python 2.7 on Linux/x86-64); what platform are you on? – Fred Foo Jul 08 '12 at 20:48
  • How do you know it is truncating? http://docs.python.org/release/2.6.7/library/stdtypes.html?highlight=readline#file.readline – copper.hat Jul 08 '12 at 20:49
  • 2
    Is it possible that there is a `\n` in the stdin stream? – dawg Jul 08 '12 at 20:51
  • @Rodrigue: that's not a duplicate. – Wooble Jul 08 '12 at 21:20
  • I would guess that @drewk's response is worth checking here. – copper.hat Jul 09 '12 at 00:24
  • 1
    Linux/x86? Are you reading directly from terminal? You might want to have a look at this question that describes lower level behavior that has nothing to do with `sys.stdin` buffering: http://stackoverflow.com/questions/18015137/linux-terminal-input-reading-user-input-from-terminal-truncating-lines-at-4095 – FooF Aug 02 '13 at 13:10
  • Let me guess - if the input line is read from file/pipe it will happily return input line over 4096 characters. But if you are reading input from terminal (interactive prompt), then it will fail at this about 4096 mark? The linked "possible duplicate" is not a duplicate in this case. – FooF Aug 02 '13 at 13:24

1 Answers1

-4
line = sys.stdin.readline()[1*1024*1024]

This works for me, when i have to read file (few MB).

Anonymous
  • 748
  • 3
  • 10
  • 22
  • 1
    I doubt it. That will give you the single byte 1048576 bytes into the first line of the file. – Wooble Jul 08 '12 at 21:15
  • @Wooble Which obviously only works if `readline` is returning a buffer of size at least 1mb+1. – Voo Jul 08 '12 at 21:54
  • @Voo: that wasn't my reading, although in any case that would make this more of a "my input isn't truncated at 4096 bytes" *comment*, rather than an answer to OP's question. – Wooble Jul 08 '12 at 23:17