Most command prompt commands not working in os.system() - python

I'm pretty new to python and I am learning how to use basic command line operations with os.system() (os module)
I haven't been able to use TASKKILL recently and I need it for a python script I'm working on.
When I try to use TASKKILL in command prompt separately it doesn't work either. Only when I open the command prompt as administrator does it work.
The error I get:
'TASKKILL' is not recognized as an internal or external command,
operable program or batch file.
I'm currently using Windows 10 and Python 2.7.13, would appreciate anyone's help.
Thank you

Related

why the same powershell command run on the powershell console but not using os.system?

I would like to include a command to create a 7zip archive withinin a Python script. Since I am working on Windows, I need to pass the command to the powershell console. I am planning to do it with os.system (I am aware that this is not the best way to do it and that I should use subprocess, but I really just need a quick fix and it would not be time effective for me to learn to use a new module in this context).
The following command works if run from the powershell console
&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch
So I recreate the same string within python like this:
cmdl = r"&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch"
The string is interpreted as follow:
"&'C:\\\\Program Files\\\\7-Zip\\\\7z' a -mx=0 X:/myarch.zip X:/myarch"
Now, if I copy-paste the above string within the powershell console, it runs without problems. However, if I run it within python using os.system(cmdl) I got the following error
"The filename, directory name, or volume label syntax is incorrect"
Why is this the case and how can I fix this issue ?
os.system is meant for executing cmd commands, cmd commands can be ran in powershell maybe after all powershell is a bit advanced but I'm sure that you can't run a cmd command in powershell, henceforth your code is not working.
However a creative solution for executing a powershell command from python(not using python) would be to write your command into a .ps file(powershell script)and then run it using os.startfile()(use this code: os.startfile("script.ps"))

Unable to call bash script using os.system() on Windows

I have a very simple bash script test.sh as shown below
#!/usr/bin/env bash
mkdir "/c/AAA"
I want to execute this code in python. When I call os.system(r"Y:\test.sh") in python, a window pops up and asks me which program I want to open the test.sh with. Then python will end with output 0 and no folder is created in my C drive. I can't find any solution online. Any help will be appreciated. :)
os.system() will invoke your command the same as windows cmd would, in this case, the windows doesn't know how to execute *.sh files, so it opens it's default dialog so you can pick one program that you know can ran it.
The same will happen if you open windows terminal and try to invoke such file.
If your windows have a bash interpreter try invoking it like this:
os.system("bash Y:\test.sh")
Instead of running this with a native-Windows Python interpreter, run it with a Cygwin copy of Python, which has an os.system() that will be invoked with the Cygwin /bin/sh.

How do i run python file in cmd from vscode

I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html

how to write command in command prompt (cmd) in windows 10 from python script with subprocess module

I need to launch external program from python script, which can be run in command prompt. I've been searching through python documentations and stack overflow but can't find anything helpful to me. I successfully launch cmd with following script:
But I still need to write down more commands like that:
mkdir data
copy data.txt c:\data
I think this is a very easy job with subprocess module but I can't find the way. How can I do this?
Try to use call
subprocess.call(['cmd.exe', 'mkdir data']);

Why Python can not execute the cmd command 'tskill'?

In general, we can use Python to execute Windows's cmd command, for example:
os.system('ipconfig')
but I find that tskill can not be executed by Python, if I use:
os.system('tskill 8684')
to kill a process by its pid, Python will show cmd's error:
'tskill' is not recognized as an internal or external command, operable program or batch file.
but it work well if I use cmd to run the command.
As I know tskill.exe is located in C:\Windows\System32, but this path is not in Python's environment variable. It is maybe the reason, but ipconfig.exe is also in System32, it can be executed.
So why tskill can not be executed by os.system or subprocess.Popen?
I have found the root reason:
My Python is 32-bit, while My PC is Windows7 64-bit, so Python's os.system can not run tskill. If I use Python 64-bit instead, everything is OK.
Use taskkill, which can do pretty much everything as tskill
But if you want to stick to tskill.exe in your scripts/code. Please run the scripts from elevated command prompts. (Right click on cmd.exe and run it as administrator)
os.system('c:\windows\system32\tskill.exe 8684')

Categories