Raw_input in cmd Python [duplicate] - python

This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 9 years ago.
I have problem with raw_input command. I use it in program and run the program in cmd. Then it asks input but when I paste there text which has empty lines in it, then python takes it automatically as enter command. I want it to understand that it's not enter command but just new line. How could I do this?

To do that, you can't use raw_input function, because this function finish reading input after first new line character. Instead you can use sys.stdin:
>>>import sys
>>>input = sys.stdin.read()
hi
this text has
new lines
Ctrl^D
>>>print input
hi
this text has
new lines
To finish reading input you'll have to send EOF char, which in Linux is Ctrl+D and in Windows is Ctrl+Z
Hope this helps!

as per python documentation, raw_input() is designed to have \n as the key that ends the input, and it has not been designed for another way of interacting.
If you want to use another way of using the input you can try using fileinput() or sys.stdin.read() and iterate over the lines or the characters and create the behavior you're looking for.
Have a look at the answers of questions such as that one

Related

how to take multi line string input from user in python,without breaking the input [duplicate]

This question already has answers here:
How to get multiline input from the user [duplicate]
(5 answers)
Closed 3 years ago.
we face the problem while taking multi-line input for strings in python.
s=str(input("enter string"))
when i enter the input
for suppose let us assume that i entered my input as:
hello friends!!
good morning
when i try to print this i get only 'hello friends!!'
is there any other code to take until i press a special symbol to tell to compiler that it is EOF of my input
like in c we have code like scanf("%[^~]",str);
it takes the input until we press "~" symbol.
like this do we have any special operations in python.
expected output of above program is
hello friends!! good morning
This has been asked here many times. Please use the search function or click here: How to get multiline input from user
tldr:
import sys
msg = sys.stdin.readlines()

Removing printed lines in Python 3 [duplicate]

This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().

Read python shell [duplicate]

This question already has answers here:
Taking input from sys.stdin, non-blocking
(7 answers)
Closed 6 years ago.
Is there a way to check if there is anything entered in the shell without using raw_input?
For example instead of having something like question = raw_input("Enter Q to quit") I just want to read if anything is entered into the shell, "Q" and quit.
The reason I need this is because I don't want to block my program from executing with the raw_input. I need it to periodically check at any given point if "Q" is entered at all.
Something like this might work
import sys
data_input = sys.stdin.readlines()
print "Counted", len(data_input), "lines."

Stop Python input when a specific character is entered [duplicate]

This question already has answers here:
How to read a single character from the user?
(26 answers)
Python sys.stdin.read(1) in a while(True) loop consistently executes 1 time getting input and multiple times not getting input
(2 answers)
Closed 6 years ago.
I'm writing a Python program (on OS X/Linux) in which a user has to type some words in a while loop. This is the relevant part of it:
i = 0
array = []
while (i < 10):
array.append(raw_input("Prompt: "))
i = i + 1
However I'd like to stop the input as soon as the user types a SPACE, without the need to press ENTER.
I know I can't do this using raw_input, however I did not find any way to achieve what I'm trying to do. Basically I would like the user to be able to type one word after the other, using the SPACE button instead of ENTER to input the next word.
Do you have any idea? Thanks in advance.
It's not possible with raw_input, input or sys.stdin.read(1) either. There is no generic solution which works on every operating system.
Here is how to read a single character from the user in Python, without pressing Enter: Python read a single character from the user
Then you need to process each character in a for loop.

How can I show never ending dots ("...") in a while loop [duplicate]

This question already has answers here:
Loading animation in python
(3 answers)
Closed 7 years ago.
Is it possible to show something like this in Python?
Checking.
Checking..
Checking...
Checking.
I want it to just show one line of this while the script is running, but stop when it's done. The script I want to add it to is here: https://github.com/brandonusher/Python-Scripts/blob/master/check_port.py
Output the text with no newline character, then flush stdout. Output a \r to make the cursor go back to the beginning of the line. Repeat until done. Don't forget to overwrite existing text.
while True:
sys.stdout.write('\rfoo. ')
sys.stdout.flush()
delay(100)
sys.stdout.write('\rfoo.. ')
sys.stdout.flush()
delay(100)
sys.stdout.write('\rfoo...')
sys.stdout.flush()
delay(100)

Categories

Resources