'Is it possible to write to Command Prompt with python?
Im opening Command Prompt with
os.startfile('C:\\WINDOWS\\system32\\cmd.exe')
and after opening the program id like to write the python file for it to run
C:\Users\user\Documents\Python>examplefile.py
when python opens the command prompt it starts with
C:\Users\user\Documents\Python>
so I would just need to add on the file to the end of the line and run it, is this possible?
Solution 1:[1]
You can use the os module to open cmd and the keyboard module for writing in cmd
import os
import keyboard
import time
os.system("start cmd")
# Because we don't want the `keyboard` module to write before cmd gets opened.
time.sleep(0.1)
keyboard.write("Anything you want")
Solution 2:[2]
I would suggest you to use os.startfile (this link is for 2.7 documentation)
Otherwise importing keyboard and using keyboard.write("python3 myfile.py") should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S.B |
| Solution 2 | FLAK-ZOSO |
