I would like to run a script like in the picture in the Powershell but from python. [1]: https://i.stack.imgur.com/K7XGI.png For instance, I would like to have a code like this:
def run(namefile):
command that opens powershell
command that type in the powershell .\spim.exe .\namefile.txt
command that run the script
Does someone know how to do this ?
If you just want to run the exe file you can use subprocess module.
If you really want to run it inside the Powershell terminal you can run the Powershell binary with -Command option.
Related
When I execute "Get-WindowsOptionalFeature -Online" command in Windows2016 server powershell, the output is all fine, but when I execute with the Python script below,
cmd = os.popen("Get-WindowsOptionalFeature -Online")
print(cmd.read())
the output is empty.
is there something wrong with my code?
I tried to exec ipconfig using this code, it worked fine
My error comes from my ignorance of powershell. I regard powershell as cmd, so I directly use os.popen to call the command to be executed. In fact, you need to call powershell first to execute the command that needs to be executed, the following is an example:
import subprocess;
process=subprocess.Popen(["powershell","Get-WindowsOptionalFeature -Online"],stdout=subprocess.PIPE);
result=process.communicate()[0]
Recently I am playing around with Rich. It's really helpful while debugging and tracking code running progress. However, if I use task scheduler to auto-run python script, it will open command prompt to run the script instead of others like Window PowerShell. All the output from Rich will not show in command prompt. Is there anyway to set python.exe run by other instead of command prompt?
My batch file looks like this:
"C:\Python39\python.exe" "C:\PATH\TO\PyScript.py"
Yes, there is a way to do this.
Just use this command:
powershell.exe "C:\Python39\python.exe C:\PATH\TO\PyScript.py"
If I'm in the Python IDLE editor and the shell is not open, is there some way to open the shell without running a program? I expect it's something simple that I just can't find.
Thanks
For Python 3.8 its just Run -> Python Shell if I am understanding your question correctly
For windows:
Win+R to open run window
cmd to open, well, the command line
python to run python. Make sure you've added the python.exe file to PATH
Can I run a script or a .exe file every time the command prompt is executed from various methods?
like maybe through Run or the toolbar in Explorer or anywhere.
I want to run a script and show something in the command prompt as an output.
If you are asking to run Python script/code on cmd then run this code:
import py_compile
py_compile.compile(filename.py,output_name.pyc)
This will create a .pyc file which you can then run in cmd using command:
python output_name.pyc
note: filename and output_name are both strings that should include fileextensions and you can write any name here.
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.