eval string

B

Brandon Harris

Needing to pass a string command into a third party program and having
issues creating a string to do what I need.

here's what I have so far.

eval('import sys;
sys.stderr.write(\'\n\n\nCompleted!!!\nCompleted!!!\nCompleted!!!\nCompleted!!!\nCompleted!!!\nCompleted!!!\n\n\n\');')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
sys.stderr.write('
^
SyntaxError: EOL while scanning string literal


so I've changed it up every way I can think and I get other errors.

eval('import sys;
sys.stderr.write("\n\n\nCompleted!!!\nCompleted!!!\nCompleted!!!\nCompleted!!!\nCompleted!!!\nCompleted!!!\n\n\n");')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
import sys; sys.stderr.write("
^
SyntaxError: invalid syntax


Brandon L. Harris
 
P

Peter Otten

Brandon said:
Needing to pass a string command into a third party program and having
issues creating a string to do what I need.

here's what I have so far.

eval('import sys;
sys.stderr.write(\'\n\n\nCompleted!!!\nCompleted!!!\nCompleted!!! \nCompleted!!!\nCompleted!!!\nCompleted!!!\n\n\n\');')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
sys.stderr.write('
^
SyntaxError: EOL while scanning string literal


so I've changed it up every way I can think and I get other errors.

eval('import sys;
sys.stderr.write("\n\n\nCompleted!!!\nCompleted!!!\nCompleted!!! \nCompleted!!!\nCompleted!!!\nCompleted!!!\n\n\n");')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
import sys; sys.stderr.write("
^
SyntaxError: invalid syntax


Brandon L. Harris

Python discriminates between statements and expressions. eval() can only
deal with expressions, but 'import whatever' is a statement. Try

exec 'import sys\nsys.stderr.write("completed!\n")'

instead.
 
P

Peter Otten

Peter said:
exec 'import sys\nsys.stderr.write("completed!\n")'

Oops, you need to escape the backslashes for newlines inside quotes inside
quotes:
completed!

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top