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)
Related
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'
I guess the title sufficiently sums up my question. I have working code to automatically check out a file:
p = Popen(['cleartool', 'co', pathname], stdin = PIPE)
p.communicate('comment for checkout')
I'm wondering how to check if the file is already checked out before executing this. Thanks in advance everyone!
You can parse the output of a cleartool ls -short pathname
If checked out, its version will end with /CHECKEDOUT.
Or you can go ahead, try to check out and test the exit status of the command. But there could be other causes for failure (other than "already checked out")
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
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
Update: When I use the subprocess.call instead of subprocess.Popen, the problem is solved - does anybody know what's the cause? And there came another problem: I can't seem to find a way to control the output... Is there a way to redirect the output from subprocess.call to a string or something like that? Thanks!
I'm trying to use Devenv to build projects, and it runs just fine when i type it in command prompt like devenv A.sln /build "Debug|Win32" - but when I use a python to run it using Popen(cmd,shell=true) where cmd is the same line as above, it shows nothing. If I remove the |, change it to "Debug" only, it works....
Does anybody know why this happens? I've tried putting a \ before |, but still nothing happened..
This is the code I am using:
from subprocess import Popen, PIPE
cmd = ' "C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv" solution.sln /build "Debug|Win32" '
sys.stdout.flush()
p = Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE)
lines = []
for line in p.stdout.readlines():
lines.append(line)
out = string.join(lines)
print out
if out.strip():
print out.strip('\n')
sys.stdout.flush()
...which doesn't work, however, if I swap Debug|Win32 with Debug, it works perfectly..
Thanks for every comment here
There is a difference between devenv.exe and devenv.com, both of which are executable and live in the same directory (sigh). The command lines used in the question and some answers don't say which they want so I'm not sure which will get used.
If you want to call from the command line then you need to ensure you use devenv.com, otherwise you're likely to get a GUI popping up. I think this might be the cause of some (but not all) of the confusion.
See section 17.1.5.1. in the python documentation.
On Windows, Python automatically adds the double quotes around the project configuration argument i.e Debug|win32 is passed as "Debug|win32" to devenv. You DON'T need to add the double quotes and you DON'T need to pass shell=True to Popen.
Use ProcMon to view the argument string passed to devenv.
When shell = False is used, it will treat the string as a single command, so you need to pass the command/arugments as a list.. Something like:
from subprocess import Popen, PIPE
cmd = [
r"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv", # in raw r"blah" string, you don't need to escape backslashes
"solution.sln",
"/build",
"Debug|Win32"
]
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out = p.stdout.read() # reads full output into string, including line breaks
print out
try double quoting like: 'devenv A.sln /build "Debug|Win32"'
Looks like Windows' shell is taking that | as a pipe (despite the quotes and escapes). Have you tried shell=False instead?