Why does running "os.system('cmd')" do nothing in Python? - python

I am trying to run Command Prompt commands using Python, but running my command seems to do nothing. The full command I pass to os.system is:
'cmd /c "apt.bat"'
This should run apt.bat, opening another program in the process. However, nothing happens. When I pass that same command to the Command Prompt manually, it runs correctly. So to try to hone in on the problem, I have tried running:
os.system('cmd')
but nothing happens. As far as I know, this should open the Command Prompt, but I could be wrong about that. To ensure that I have the correct syntax and that os.system isn't broken or something, I have also tried running:
os.system('notepad')
which opens notepad without issue. Is there something different about 'cmd' that would cause it to simply not open? Is there some syntax I am missing? Maybe permissions?
For reference, I am on Windows 10. Python 3.7, using Spyder 4.

Your problem is that cmd is not a command. You should be using os.system('start cmd') or subprocess.run('start', shell = True) using the subprocess module.

Syntax: os.system(command)
command: It is of string type that tells which command to execute.
Return Value: On Unix, the return value is the exit status of the process and on Windows, the return value is the value returned by the system shell after running command.
Thus when you run the following command from the python shell
os.system("cmd")
you get this output:
Which is similar to the output you get when you run the same command from the command line as well:
I.e. The return value is the value returned by the system shell after running command.
Thus to "run the command prompt using python", do this:
os.system("start cmd")
Update
To run the specific command that you are trying you simply need to remove the single quotation marks, try this:
os.system("cmd /c apt.bat")

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

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

using bash commands in python on mac: error 127

I am using am using Python 2.7 on MacOS and want to use a bash command within a python script.
command = "someProgram --option1 value 1 --option2 value 2"
I had to include the path of this program in my bash_profile in order to run it. I tested so far:
os.system(command)
and
subprocess.check_call(command.split(" "),shell=True)
Neither worked. The latter threw error 127 and the first one only returned 32512. A google search told me that this occurs when the command is not known.
If I now start this command within the terminal everything works perfectly fine.
Do I have to include something such that python can find this command? Why is this behavior?
With shell=True the cmd needs to be a string.
subprocess.check_call(command, shell=True)
where command is of type str
Thanks for your help. The final solution is kind of stupid. I started spyder via the anaconda GUI. If I do so the above code does not work.
If I run this directly via the console or start spyder via the console everything is fine. It seems that the bash_profile is not loaded when spyder is loaded but requires the console to do so

python os.popen does not work with a command that works in cmd prompt

I am able to run the following using os.popen
x = os.popen('dir')
but the following does not work
x = os.popen('cmd /C ""C:\Program Files (x86)\Ixia\Tcl\8.4.14.0\bin\wish84.exe" "C:/Users/lab/Documents/Public/TCL/Scripts/GetStatsFirst2.tcl""')
I have run the exact command on the windows command line, and it manages to open the TCL wish console and does everything. It just doesn't work with os.popen. It is supposed to open a TCL wish console, run the script and close. Is there a way to make this work?
Also, I have tried subprocess.call with this command and it returns 1 and does nothing.
subprocess.call(['cmd', '/C', 'C:\Program Files (x86)\Ixia\Tcl\8.4.14.0\bin\wish84.exe', 'C:/Users/lab/Documents/Public/TCL/Scripts/GetStatsFirst2.tcl'], shell = False)

Is there a way to capture the MSDOS command prompt string?

In Windows XP when you open cmd.exe you get a console window with a command prompt looking like:
"C:\User and Settings\Staffer\My Documents>" where s> the underscore after the '>' is the cursor.
This is the default for Windows XP. A user might change it using the PROMPT=something or by using set PROMPT=something
In the console window, at the command prompt, entering the internal command "prompt" with no arguments does not return what the current prompt string is.
Is there a command or preferably a Python library that can retrieve what the command prompt is. I didn't want to write a Python module if there was a builtin way of retrieving that string.
The use case for getting the command prompt string is when I use the Python subprocess module to run a python program, and then return to the same console's command prompt while the subprocess is running, I get the cursor on a blank line. I can press Enter and the command prompt will redisplay; but it looks as if hasn't returned from the subprocess yet, which misleads my users.
One solution for the gui part of my app is to run pythonw runapp.py. However I'm left wondering if there's a way to get a clean command prompt when calling subprocess by using already made DOS commands, Python library, proper use of subprocess.Popen() and communicate()?
Not sure if it helps but if you enter "SET" from the command prompt you'll see a list of environment variables, including the current PROMPT (however it won't appear in the list if it's the default prompt).
From the command line:
c:\>echo %prompt%
$P$G
From Python:
>>> import os
>>> os.environ["PROMPT"]
'$P$G'
(http://docs.python.org/library/os.html#process-parameters)
[edit]
Ah, I missed your edit. It sounds like all you want to do is run the script in the background. I believe you are looking for the Windows "start" command with the /b option - http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/start.mspx?mfr=true
I think you are looking for this:
import os
print os.getcwd()

Categories