Is it possible to write to Command Prompt with python? - 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?

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

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

Is there a way to run a random .bat file from a folder using python WITHOUT opening a new cmd window?

Alright so well,basically im trying to run a random bat file from a folder without opening a new command prompt window by using python(i open the python file and then the bat file that is choosen by the python file will run in the same cmd window) ,as soon as the bat file runs all the code it had,it will run the python file again in that same cmd window,I've tried using this https://stackoverflow.com/a/54110207/18990761 but it opens a new cmd window instead of using the same one so yeah,Sorry for my grammar english is not my first language.
If you don't want your program to open a command window, Import all your commands to the python and run it from python.
Example:
# importing os module
import os
# Command to execute
# Using Windows OS command
cmd = 'notepad'
# Using os.system() method
os.system(cmd)

How to launch python from within python Windows 10)?

I am running python3 in Windows 10. Within my program, I try to launch another python instance.
os.system("python -m http.server")
print("All Done")
or
os.system("python")
I want to see a Window shell that pops up and allow me the see the output and interact with it. But instead the program just continues. I don't even see a window pops up. What am I missing?
If you want to open a new window, you need to use start cmd
If you want to open a new window running a command from cmd, that's start cmd /c "command"
e.g
import os
os.system('start cmd /c "python -m http.server"')
However, if you really just want to run another python process, you can import and run the server directly from code - refer docs

Python open a text(notepad) document

Im new at programming and i was wondering how can i open a notepad document so the user can type on it. I already know that you can use
variablename = open("filename.txt","w")
to open and write on a file but instead of writing on python i wanted the file to open directly so the user can type on the actual file not in python shell.
So far i know that i have to use
import os
os.?????(filename.txt)
but i dont know how to make the file pop up so the user can enter data. can somebody help me?
This easiest approach using os is to use os.system to run a shell script:
import os
os.system("notepad filename.txt")
Or using subprocess.Popen which is usually the recommended way:
import subprocess
subprocess.Popen(["notepad","filename.txt"])
# the concepts of both my methods is they run a shell script calling notepad to run filename.txt
But I believe only Windows has the Notepad application.
You can also use the suggested method from the comments:
import subprocess
subprocess.run(["notepad","filename.txt"])
But that only works in Python 3.5+
from os import startfile
startfile( "filename.txt" )
this will open your file in your system's default editor for the file type.
import os
os.system("start notepad.exe <path/to/file>")
This will pop-up Notepad. If the file doesn't exist, there will be a prompt asking if you'd like to create it.

Why does my python bash subprocess show a cant open file error?

I have a python tile menu script where a subprocess executes a bash shell:
# excute shell script
subprocess.call([self.path + '/pimenu.sh'] + actions)
and in the shell I have:
python ./to/file/name/"$*".py
but when the python script which is found and executed returns an error as it cant find the folder with the images in.:
pygame.error: Couldn't open ./file/name/image.jpg
I am assuming it is looking in the folder the menu script is in, how can I give python or bash the correct path to the scripts resources?
You can use a path finding built in python's os package into the scripts ./to/file/name/"$*".py like this:
import os
print os.getcwd()
It is good enought described into the next SO answer: https://stackoverflow.com/a/3430395/2261861

How to start child cmd terminals in separate windows, from python script and execute scripts on them?

I have been trying rather unsuccesfully to open several terminals (though one would be enough to start with) from say an ipython terminal that executes my main python script. I would like this main python script to open as many cmd terminals as needed and execute a specific python script on each of them. I need the terminal windows to remain open when the script finishes.
I can manage to start one terminal using the command:
import os
os.startfile('cmd')
but I don't know how to pass arguments to it, like:
/K python myscript.py
Does anyone have any ideas on how this could be done?
Cheers
H.H.
Use subprocess module. Se more info at. Google>>python subprocess
http://docs.python.org/2/library/subprocess.html
import subprocess
subprocess.check_output(["python", "c:\home\user\script.py"])
or
subprocess.call(["python", "c:\home\user\script.py"])

Categories

Resources