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
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 have my code which includes few lines:
print("python kaldigstserver client.py -r 32000 ",self.filename,file=open("output.txt", "a"))
with open('output.txt') as my_file:
file_text = my_file.read()
print(file_text)
subprocess.Popen(args=["gnome-terminal", "--working-directory=/home/huma/G-str/kaldi-gstreamer-server"])
This opens a terminal when I execute my python program...... But I want to redirect the command from the text file to the terminal.... i.e. output.txt. and execute it automatically.
I don't know how to do that. I tried it with "--command"& "--execute" but it is not working. Is there any way to do it ????
Thanks in advance.
It can be done one has to use bash output.text as the command argument like this '--command="bash output.text"'. Note the quotes around the argument are required.
subprocess.Popen(["gnome-terminal", "--working-directory=/home/huma/G-str/kaldi-gstreamer-server", '--command="bash output.text"'])
If the output.text is not in the set working directory, the full path must be given.
One can replace bash with any other shell that supports the same expected command language.
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)
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
I have the following python script which i want to run..
However, it keeps showing the error message on my command prompt whenever i attempt to run the script.
Error message:
File "xor.py", line 9
File = open(sys.argv[1], 'rb').read<>
SyntaxError: Invalid Syntax
The following is the command i executed in cmd:
python xor.py sample_output.txt 'what would the secret be?'
The following is the script:
# xor.py
import sys
from itertools import cycle
file = open(sys.argv[1], 'rb').read()
string = sys.argv[2]
sys.stdout.write(''.join(chr(ord(x)^ord(y)) for (x,y) in zip(file, cycle(string))))
You are not running the code you are editing, instead you are running a different file than the one you edited.
This is because there is no syntax error in the code that you have provided. However, there is a syntax error in the code in the error message:
File = open(sys.argv[1], 'rb').read<>
This ends with <>, not with (). I assumed this to be a transcription error, but you say that the error message really appears like this, although the code does not.
Hence: You are running a different file than the one you are editing.
You have .read<> when you probably intended .read()
First of all, "file" is already reserved; that is built-in keyword so unable to set as the name of variable.
And second, do not use <> instead of (). incorrect in grammar.
The problems might be clearly solved if you code like:
fd = open(sys.argv[1], 'rb').read()