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

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

Related

How to detect if Python is running in Git Bash terminal, or Windows cmd?

I'd like to give my Python scripts the ability to detect whether it was executed in a Git Bash terminal, or the Windows cmd command line interface. For example, I'm trying to write a function to clear the terminal (regardless of which terminal it is), e.g. echoes the clear command if in Git Bash, or cls if in cmd.
I've tried using sys.platform to detect this, but it returns win32 regardless of which type of terminal it was ran in.
Please try using os and psutil modules.
For example,
import os, psutil # Get the parent process name.
pprocName = psutil.Process(os.getppid()).name()
Then you can have your logic depending on the shell.
Additionally, you may want to check https://www.geeksforgeeks.org/clear-screen-python/
I don't believe what you're asking for is possible, but there are several answers here that show all the detections you can do to use the correct type of clear. Usually, it's just best to either make your own window or not clear the screen, sadly.

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

Why does running "os.system('cmd')" do nothing in 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")

Program to run another through command prompt

I am not even sure if this question is answerable.
Basically in my game I am using the colorama features to make it look nice, but the colorama features only work when you access python in command prompt, so my question is how I can get python program to run another via command prompt, is this doable or not? I have tried installing win32 but that is in python 2 format and I am using 3.4 so I was getting syntax errors that I wasnt sure how to fix.
I am not sure why is this happening, I mean, colorama not working without starting from prompt.
Perhaps something with environment variables PATH or something.
This is one suggestion, and I am not sure that it will work as we will not be changing the window program is running in, just invoking cmd.exe i.e. command prompt to start within it and start python and your script again.
But it is worth a try:
# Start of your program:
import sys, os
if "started_with_prompt" not in sys.argv:
cmd = 'cmd /C "'+sys.executable+' '+" ".join(sys.argv)+' started_with_prompt"'
os.system(cmd)
sys.exit()
print "the rest of your program"
If this doesn't work, there are tricks that can be used through subprocess module to do the similar thing.
Also, you should look at cmd.exe's help to see whether you should use some other switch than /C to enable environment and/or registry extensions.
But, essentially, you should be able to get the same result by making a shortcut with the command like one in cmd variable, or a batch file that starts Python. Like this:
#echo off
cmd /C "C:\Python27\python.exe path_to_your_script.py"
I think both would work, but somehow that you wouldn't like this solution.
Well, I think that shortcut would need a full path to cmd.exe which is:
C:\Windows\system32\cmd.exe
Let me know if it doesn't work.

Run python source file from PowerShell

I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works

Categories