3

I want to open a cmd window and use it to print some variables. The problem is that, the code below, opens 5 independent windows (ovbiously).

import os for i in range(0,5): command = "start cmd /K echo " + str(i+1) os.system(command) 

I dont know if I can have a reference or a link to the already opened window to send it the commands.

The desired result would be...

1 2 3 4 5 

...on the same window

The purpose is to be able to print on that window certain variables during the execution of a code.

I'm stucked and I don't know if that is even possible. Im on W10 with Python 2.7

Thank you very much

3
  • 2
    should it be with cmd? because you could do it with python itself Commented May 14, 2018 at 10:55
  • 1
    It can be done but will be complicated. One way I can think of: you can have one Python program reading a named pipe and outputting what is gets to the console, and another Python program writing the numbers to the named pipe. Windows consoles aren't GUI windows that you can write to. Commented May 14, 2018 at 11:02
  • Typical XY problem. What the OP wants is obviously a logger. Commented May 14, 2018 at 11:28

2 Answers 2

1

What you want is actually the logging package from the stdlib. Simply configure your logger to write to a file, and in another command window use the Windows equivalent of unix's tail -f <filename> command to check what gets logged in (almost) real time.

Note that you could also just log (still using the logging lib) to sys.stderr, launch your python app from a command window and then you'll have all the logging in the very same command window (typical unix development flow).

As a last note: the logging lib is quite extensible so you could even write your own logging.handler that opens it's own windows and displays logs messages in it if you really want (but that would really be a waste of time).

Sign up to request clarification or add additional context in comments.

2 Comments

The environment of the app is very complex, I cannot launch the python app via command window because is executed on a web as a python plug-in, and I don't have much control once is launched. But yes, the logging class may solve my problem. Thanks!
The most complex the environment, the more likely logging is the right tool. The most important thing to understand with logging is that the configuration should be done by the application code, not by libraries (you can have a look at how django configures logging for a working example).
0
import os command = "start cmd /K echo " for i in range(0,5): command += str(i+1) os.system(command) 

Echo is not handling line breaks well from version to version. So this outputs the sequence 12345

2 Comments

Well it doesnt output 12345 literaly. But echo returns 12345 , i think.
I know, I just tried to simplify the code to focus on the code. But yes, the output would 12345 for that code in case it could be launched on a single window

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.