How to open program with key input - python-3.x

I want to open the calculator program when I type "Open calculator " . I researched a lot , but didn't get the answer I wanted.Can anyone please answer my question.

There are 2 ways to do this. One way would be access the cmd using Python which can be done by using os module. When you try to open the calculator from your command prompt, you probably type calc. Instead of manually doing this, you can have your Python code do it for you, this is how:
import os
os.system('calc')
The second way is very similar to the first one, except that this method opens another command prompt window so the window in which you're running the python code is not disturbed.
import subprocess
subprocess.Popen("calc",stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,shell=True)

Related

Python file immediately closes

As an assignment for one of my classes, the professor had us download a file containing python code as well as a couple of text files that the python code will analyze.
The problem is, when I click to open the python file, the command window opens, then immediately closes. I can get it to stop from closing if I click on it fast enough, but that stops the code from loading, and any keystroke causes it to close again.
It's the same file every other student downloaded and used with no issue. I've tried re-downloading the file, re-installing python, even trying on a different computer. I've also tried opening it by entering the path file name in the command window with no success. Also no luck trying it in Jupyter Notebook or CodeLab. (And of course, no luck experimenting with the slew of possible solutions on previous Stack Overflow, Reddit, etc. questions).
Any ideas on the potential cause and/or solution?
Edit: cause unknown, but the solution was opening the file with Sypder console.
the file closes immediately because It ended and returned 0, to stop that from happening you can add at the end of the python code something like that:
ending = input("press key to exit")
or just import time and use
time.sleep(5)
you can open your file in the cmd/terminal window and you will be able to see the results

batch file to close cmd when program not found

I am writing a python program which takes input from the user then create a batch file and open that program and I want that if program not found then it returns that error to python and close the popup
Windows cannot find 'room'. make sure you type correctly, and then try again.
and cmd.
I don't have any idea how to solve this problem.
def batch(text):
bat_file = open("hello.bat","w")
bat_file.write("start "+text)
bat_file.close()
os.startfile("hello.bat")
As I mentioned above I want to close the popup and cmd.

I can't get pythons output viewer to run

I just downloaded python on a new pc and now whenever I try to create a script the output viewer just closes down immediately when I launch it as a python file.
My original script was
import secrets
secrets.token_hex(32)
but it just closes down immediately. I thought that it was something with my code so I tried to just make the simple "hello world" script.
(print) "I hope this work"
and I had the same result as the first script, the output window opened up then immediately closed down.
I can get it to work using the python shell but I prefer doing all my coding using notepad++ and it would a real pain in the behind if I can't get that to work.
This is expected behavior if you run your scripts as you described, by opening them in Explorer. Your script completes execution in a terminal window, then closes immediately.
If you absolutely insist on running them on double click and still want to see your console, I suggest you create a .bat file at your python path with contents like this:
python -i %1
and them bind your double-click handler to use that file on .py extension. That way, Python will execute your file and go into CLI mode, preserving your terminal window and allowing you to type further commands.
My other guess would be that you want a console plugin to work right within Notepad++, in that case use nppexec: https://sourceforge.net/projects/npp-plugins/files/NppExec/

python3 input using sublime text 3

I am trying to use the input feature on python for my program. I installed SublimeREPL but I still cant seem to figure out how to provide an input for my program. (coming from a complete beginner)
Using input from within an editor can be tricky. A simple solution would be to run your program from the command line. Open a terminal and change into the directory where your script user_inputs_intro.py is located and type:
python user_inputs_intro.py
Now, you will see the Tell me something: and should be able to type something that will be echoed back after you press enter.

Clearing Python shell

How to clear Python shell ?
I am writing a module in python, I want to save it in a file. what is the best way to do it?
File -> New Window. Put your module in this new window, than save it. To run, just press F5.
Python shell does not get cleared or saved. Perhaps you are using IDLE. It's a confusing piece of software. I'd recommend you to get a real IDE, or at least a proper text editor.
Something that I do in IDLE is print "\n"*100
You can also extend Idle and map that to a key. Read Pythoninstall\lib\Idlelib\extend.txt for details
http://bytes.com/topic/python/answers/737111-extension-idle-clear-screen-but-how-write-screen
If you are just in console, you can do ( which won't work in IDLE):
import os
os.system("cls")
Just copy and paste the code into a new file (In file), and save it. To run, you can go to the run section and select "Run Module", or you can simply press F5.

Resources