Call a cmd.exe bat.-like command with python - python

i need some help with this...
I have a program installed on my computer that i want to call to calculate some things and give me an output-file...
in Matlab the command "dos()" does the job giving me also the cmd screen output in matlab.
I need this to work in python but i am making something wrong.
data='file.csv -v'
db=' -d D:\directory\bla\something.db'
anw='"D:\Program Files\bla\path\to\anw.exe"' + db + ' -i' + data
"anw" output is this one:
>>> anw
'"D:\\Program Files\\bla\\path\\to\\anw.exe" -d D:\\directory\\bla\\something.db -i file.csv -v'
## without the "" it does not work either
import subprocess as sb
p= sb.Popen('cmd','/K', anw) ## '/C' does not work either
i get the following error message from cmd.exe inside the python shell
Windows cannot find "\"D:\Program Files\bla\path\to\anw.exe"" Make sure you typed the name correctly, and then try again.
this line runs when i make a bat. file out of it.
it runs in matlab via "dos(anw)" so what is wrong here?
ps: i have blanks in my command... could this be the problem? i do not know where the first "\" comes from in the cmd. exe error message

for now i created a bat. file with all the stuff cmx.de should do in the specific directory where the input file lies...
i just had to tell python to change directory with
import os
os.chdir("D:\working\directory")
os.system(r'D:\working\directory\commands.bat')
it works good and gives me the output of cmd directly in the python shell

Related

How can I use Python to open command prompt, change directories then pass a string that contains a variable?

First time ever asking for help here so my apologies if i'm missing something relevant.
I am learning Python for data science (very new), but figured I could use it to automate some of my other work.
I'll post what I have so far, but first I'll explain what I'm trying to do:
Normally (without Python) I open up the command prompt, change directories, and then paste a string (directly into command prompt) that looks like:
"program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD"
here is what I have so far:
import os
mail_date = input(("What is the mailing date?(YYYY-MM-DD): "))
os.system("start cmd.exe #cmd /k CD C:\Path")
# Next string would look like:
# "program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD
So far I can run the Python script, input a mailing date as a variable, open command prompt, then change directories. I am not sure how to then pass the "program.exe..." string to the command line.

How to run a url from inside a python script to command line?

I have this command line text that I want to run inside my python script to the command line. Any suggestions on how to do this?
explorer "ms-drive-to:?
destination.latitude=47.680504&destination.longitude=-122.328262&destination.name=Green Lake"
If you want to effectively run this in python shell,like this: # it will open windows map
>>> !explorer "ms-drive-to:?
destination.latitude=47.680504&destination.longitude=-122.328262&destination.name=Green Lake"
If you want to run in code, do like others method.All the answers are correct.
import os
command_str = 'explorer "ms-drive-to:? destination.latitude=47.680504&destination.longitude=-122.328262&destination.name=Green Lake"'
os.system(command_str)
# it will open windows map, and driving directions to Green Lake from your current location.
Be careful to use double quotes, single quotes will not be recognized correctly!
windows uwp info: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/launch-maps-app
Sure, you can do something like is described in this post:
https://raspberrypi.stackexchange.com/questions/17017/how-do-i-run-a-command-line-command-in-a-python-script
Seems like you should use subprocess:
Running Bash commands in Python

Python OS Commands Not Work Due To New App Execution

I need to check GoldenGate processes' lag. In order to this, I execute Goldengate than I try to run GoldenGate's own commands "info all".
import subprocess as sub
import re
import os
location = str(sub.check_output(['ps -ef | grep mgr'], shell = True)).split()
pattern = re.compile(r'mgr\.prm$')
print(type(location))
for index in location:
if pattern.search(index)!=None:
gg_location = index[:-14] + "ggsci"
exec_ggate = sub.call(str(gg_location))
os.system('info all')
Yet, when I execute the GoldenGate it opens a new GoldenGate's own shell. So, I think because of that, Python cannot be able to do run "info all" command. How can I solve this problem? If there is missing information, please inform me.
Thank you in advance,
For command automation on Golden Gate you have the following information in the Oracle docs: https://docs.oracle.com/goldengate/1212/gg-winux/GWUAD/wu_gettingstarted.htm#GWUAD1096
To input a script
Use the following syntax from the command line of the operating system.
ggsci < input_file
Where:
The angle bracket (<) character pipes the file into the GGSCI program.
input_file is a text file, known as an OBEY file, containing the commands that you want to issue, in the order, they are to be issued.
Taking your script (keep into mind I don't know to code into python) you can simply execute a shell command in python in the following way:
import os
os.system("command")
So try doing this:
import os
os.system("ggsci < input_file")
Changing the input_file as indicated by the docs.
I think you will have an easier time doing it this way.

Python: system command

I spend a few hours writing a little script.
Basically what it does is create a new text file and fills it up with whatever.
I zip the text file --using zipfile-- and here's where my problem lies.
I want to run the Windows system command:
copy /b "imgFile.jpg" + "zipFile.zip" newImage.jpg
To merge the image "imgFile.jpg" and the zip "zipFile.zip".
So:
os.system("copy /b \"imgFile.jpg\" + \"zipFile.zip\" newImage.jpg")
When I run my script, it all seems to go fine.
But when it's done and I try to extract the 'newImage.jpg' file, it gives me:
The archive is either in unknown format or damaged
This ONLY happens when I run the system command within the script.
It works fine when I use the shell. It even works if I use a separate script.
I've double checked my zip file. Everything is in good shape.
Is there something I'm doing wrong? Something I'm not seeing?
Have you tried using shutil?
import shutil
shutil.copy(src, dst)
There may be a problem with the way Python is passing the arguments to the shell command. Try using subprocess.call. This method takes arguments as an array and passes them that way to the command:
import subprocess
subprocess.call(["copy", "/b", '"imgFile.jpg" + "zipFile.zip"', "newImage.jpg"])

Running a command with os.system(A) produces different output then running A in shell

I have a hex to binary converter that I am using in a Python script...
os.system("./HexToBinary " + str(sys.argv[1]) + "_hex " + str(sys.argv[1]) + "_binary")
If I run my python script on file A_hex, it produces file A_binary.
But if I run this command directly in the command-line, on file AA_hex it produces AA_binary. But A_binary and AA_binary differ!
diff example_python example_shell
Binary files example_python and example_shell differ
The version produced by the python os.system call is missing a chunk of data from the end. It seems that it just gets cut off.
I'm kinda stumped. Any thoughts?
I dont know if it is needed, but try wait, http://docs.python.org/library/os.html#os.wait
also, check the output of system, it may help tell you if something is wrong

Categories