PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

[FIXED] How to read user input until EOF?

 October 29, 2022     eof, input, python, string     No comments   

Issue

My current code reads user input until line-break. But I am trying to change that to a format, where the user can write input until strg+d to end his input.

I currently do it like this:

input = raw_input ("Input: ")

But how can I change that to an EOF-Ready version?


Solution

Use file.read:

input_str = sys.stdin.read()

According to the documentation:

file.read([size])

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached.

>>> import sys
>>> isinstance(sys.stdin, file)
True

BTW, dont' use input as a variable name. It shadows builtin function input.



Answered By - falsetru
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing