How to run more than one command with Python subprocess - python

I'm trying to run three commands with subprocess.Popen(), i don't know which is the problem.
The commands are running properly on the terminal but not on the code. Here is the code and the output.
str1 = os.path.join(install_path, "components", "esptool_py", "esptool", "esptool.py")
print(install_path) #/home/laura/esp/esp/idf
print(str1) #/home/laura/esp/esp-idf/components/esptool_py/esptool/esptool.py
str_result = "error has occurred, check if the credential in the \"input.config\" are correct"
cmd1 = f"cd {install_path}; . ./export.sh; python3 {str1} flash_id"
cmd = subprocess.Popen(cmd1, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
gui.write_on_output_text("1.0", "checking database for incorrect input on device code")
out, err = cmd.communicate()
out_str = str(out.decode("utf-8"))
THE OUTPUT:
/home/laura/esp/esp-idf
/home/laura/esp/esp-idf/components/esptool_py/esptool/esptool.py
/bin/sh: 22: ./export.sh: [[: not found
COMMAND IN TERMINAL THAT WORKS PROPERLY:
cd /home/laura/esp/esp-idf ; . ./export.sh ; python3 /home/laura/esp/esp-idf/components/esptool_py/esptool/esptool.py flash_id`
I don't know why in the terminal works, but in the code not. And i don't know if the error is that is NOT FINDING THE FILE or THE COMMAND.
Thank you :)
I already tried many ways to do it.
First i used the command gnome-terminal but it was not correct, because i don't want a new terminal I just want to send these 3 commands.
I know that in the terminal works because the response after send it is good, is what i expected, but in the code is not working, and I'm not sure if it's because Python cannot find the commands on /bin/sh, or if cannot find the file "export.sh".
I have this problem with the 3rd command too, it cannot find the "esptool.py"

I fix it with one of the ideas/resolutions on the comments.
subprocess.Popen(..., **executable='/bin/bash'**)
Just adding this :) The problem as they said, is that, i was trying to run with 'sh'

Related

How to automate commands from the command prompt?

I tried doing this in python and found questions here and here that look relevant, but I couldn't get it working from those answers. I am looking for a way to automate a manual process where I first change the directory and folder from the command prompt as follows.
d:
then
cd vph
then from the command prompt I run the following:
krr filename.xdf -vph 1,129 -s "StartFrequency: " -StartFrequency -n
The last line causes a frequency to be displayed at the command prompt. How can I save to a text file the frequency the last line returns, and how can I automate all the steps above? These steps need to be automated so I can do the same for over a thousand files. I worry nobody reading this has the krr software I need to use. If necessary, you can substitute the last line for something that will run on any windows computer and return a number. The "double-quotes" in the last line might be tricky if the last line needs to be in "double quotes".
import subprocess
#list of commands to be executed can be given one after another and add && between the commands.
#Only when the first command execution is successful the next command will get executed
lst_cmd = "d && cd vph && krr filename.xdf -vph 1,129 -s 'StartFrequency: ' -StartFrequency -n"
out,err = subprocess.Popen(lst_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
print ("the output is {}".format(out))

Sending a command to terminal

Mpsyt is a terminal based library for Python. It provides to searching and playing music on youtube. This code which provides searching on youtube:
os.system("mpsyt search creep")
But then, i need to send a command to terminal which is "1". Because this "1" plays first music on searching list. How will i send "1" command to shell which is exist?
Try the following code..
import subprocess
p = subprocess.Popen(['mpsyt', 'search', 'creep'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.communicate(b'1')[0].strip().decode('UTF-8'))
You can read the output of the subprocess by p.stdout.readline() command. Make sure there is something to be printed in console before writing that command. Otherwise your will program will stuck.

Python Pexpect issue : Getting prompt between the command and result

I can see that this is not a Python/Pexpect issue but way two Linux machines communicate with each other.
But will appreciate if someone can help me find a way to handle this.
i issue the command and then expect for the prompt , get the Before and look for what i need(response) and then i move on to next command.
what is happening is that i see that after i issue a command in the before i get the command and then command prompt.
cmd = 'rm /usr/local/file'
self.myobj.sendline(cmd)
match = self.myobj.expect(self.prompts, timeout=timeout)
print self.dut_host.before
print self.dut_host.after
if i do it manually i will be getting something like
MYPROMPT> rm /usr/local/file
rm: cannot remove '/usr/local/file': No such file or directory
But what gets printed is
MYPROMPT> rm /usr/local/file <-- self.dut_host.before
MYPROMPT> <--- self.dut_host.after
and the actual output i will see in the next command and eventually killing my program.
Has anybody faced this issue and can anybody suggest a way i can tackle this.
Thanks in Advance!!
change the code like this:
cmd = 'rm /usr/local/file'
self.myobj.sendline(cmd)
print self.dut_host.before
print self.dut_host.after
match = self.myobj.expect(self.prompts, timeout=timeout)

Can someone interpret this error message?

When i run this code:
import os
openfile = open('/home/pi/cmds.txt', 'r+')
command = openfile.read()
if command != "":
os.system(command)
openfile.write("")
I get this error message:
sh: l: The: not found
anyone know whats going on?
(the eagle eyed of you might have worked out i'm running this on a raspberry pi)
The code is opening a file and trying to execute whatever is contained in the file as a shell command.
What it happens is that the content is not a valid command, and that's why there is an error.
Check the value of the command variable before you call os.system(). It's probably not what you think it is.
Here is how I can achieve a fairly similar effect with my /bin/sh:
sh-3.2$ "l: The"
sh: l: The: command not found

Call a cmd.exe bat.-like command with 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

Categories