problem with exec

Steve Holden steve at holdenweb.com
Sat Jul 21 22:31:38 CEST 2007


...:::JA:::... wrote:
> Hello,
> 
> After my program read and translate this code:
> 
> koristi os,sys;
> ispisi 'bok kaj ima';
> 
> into the:
> 
> import os,sys;
> print 'bok kaj ima';
> 
> and when it run this code with "exec", I always get error like this, but I
> still dont't know what is a problem:
> 
> Traceback (most recent call last):
>   File "C:\Python24\Lib\site-packages\VL\__init__.py", line 188, in 
> kompajlati
>     kompajlati_proces()
>   File "C:\Python24\Lib\site-packages\VL\__init__.py", line 183, in 
> kompajlati_proces
>     h2=Konzola()
>   File "C:\Python24\Lib\site-packages\VL\__init__.py", line 158, in __init__
>     k=kod(ZTextCtrl.GetLabel())
>   File "C:\Python24\Lib\site-packages\VL\__init__.py", line 83, in kod
>     exec(str_ngh)
>   File "<string>", line 1
>     import os ,sys ;
>                     ^
> SyntaxError: invalid syntax
> 
This is almost certainly because the code contains embedded carriage 
returns:

 >>> code = """import os,sys;\nprint 'bok kaj ima';"""
 >>> exec code
bok kaj ima
 >>> code = """import os,sys;\r\nprint 'bok kaj ima';"""
 >>> exec code
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<string>", line 1
     import os,sys;
                   ^
SyntaxError: invalid syntax
 >>>

> PS: How can I change when user write script with my program to he don't need
>       aspirate the lines of his source code
> e.g.
>      import os,sys
>      n=90
>      if n==90:print "OK"
>      else:print "No"
> 
>                                                                              
I'm afraid I don't understand this question. If you are talking about 
the indentation of the code, if you don't want indentation you will have 
to use braces - { and } - to indicate the nesting structure of your program.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list