Home > Back-end >  Is it possible to write to Command Prompt with python?
Is it possible to write to Command Prompt with python?

Time:05-06

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?

CodePudding user response:

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")

time.sleep(.1) # time.sleep() because we don't want the keyboard module to write before cmd open.
keyboard.write('Anything you want')


CodePudding user response:

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.

  • Related