eval string

Peter Otten __peter__ at web.de
Thu Sep 30 10:46:11 EDT 2010


Brandon Harris wrote:

>   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.



More information about the Python-list mailing list