I am working in Python 3.7.6 on Windows, running my script both in the Visual Studio Code built-in terminal and as an executable.
I am writing the user input portion of my script now and am running into trouble with the input()
prompt printing after the user input. I've been poking around on the internet and I am now suspecting that it's a buffering problem with how I've redirected my sys.stdout
.
I found this answer to be helpful for logging. Following this advice, I've created a logging object with the following code:
class Logger(object):
def __init__(self, logDir, logFile):
self.terminal = sys.stdout
self.log = open(file = (logDir + logFile), mode = "w", buffering = 1)
def write(self, message):
self.log.write(message)
self.terminal.write(message)
self.flush()
def flush(self):
#this flush method is needed for python 3 compatibility.
#this handles the flush command by doing nothing.
#you might want to specify some extra behavior here.
pass
I redirected my stdout to this logging object using the line
sys.stderr = sys.stdout = Logger(logDir = __location__, logFile = "__logInput.txt")
,
where __location__
is the log's correct directory.
With my output diverted as such, I run the code below:
answer1 = input("yes or no? (y/n) ")
if answer1 == "y":
print("answered y")
I run the code as an executable, but my terminal is blank until there is a print statement. For example, if I enter y
for the first prompt, I get the following output in my terminal:
y
yes or no? (y/n)answered y
and in my __log.txt
file:
yes or no? (y/n)answered y
You'll notice that the logger's write()
method contains a line self.flush()
. This line was one of my attempts to flush the output buffer to prevent my input()
prompts from appearing after the user's input.
I referenced this answer as well, but adding sys.stdout.flush()
calls before and after the input()
statement did not help.
I did notice, though, that modifying my prompt to include a newline
character made the prompt show up before the user input. The code:
answer1 = input("\nyes or no? (y/n) ")
if answer1 == "y":
print("answered y")
returns the desired output in the console:
yes or no? (y/n)y
answered y
but a poorly formatted logging file:
yes or no? (y/n)answered y
I read that the input()
documentation says
If the prompt argument is present, it is written to standard output without a trailing newline.
That makes sense on the output formatting. However, there was nothing explaining why I need a newline
character in the prompt.
My question is why I'm seeing the behavior I'm seeing and whether it's possible to get the prompt to print correctly, before the user's input, without having to add a newline
character? I'd like to format my user input prompts a certain way, and these newlines
are making it challenging.
Thank you!!