hexagon logo

Problem with python code in macro called from Adams/Car 2018

Hello,
 
I am trying to run python code from the macro in the Adams/Car 2018.
 
The python code is following:
 
!--- Run PYTHON code
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("import glob, os, os.path")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("cwd = os.getcwd()")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("filelist = glob.glob(os.path.join(cwd, 'tmp_*'))")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("for f in filelist:")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("   os.remove(f)")))
!
 
When the python code is called in Adams/Car 2018 then I get following error message in the log file:
 
!
! ---------------------------------------------------
!  File "<string>", line 1
!    for f in filelist:
!                     ^
! SyntaxError: unexpected EOF while parsing
!  File "<string>", line 1
!    os.remove(f)
!    ^
! IndentationError: unexpected indent
 
When I run the same python code from DOS shell with command: adams2018 python ***.py then the code runs without any problem.
 
What could be the problem?
 
Can a python for loop be started in macro with (eval(run_python_code(…)))?
 
Thanks and best regards,
Ondrej
Parents
  • Hi Kent,
    thanks a lot for your answer it helped a lot.
    Both your ways are working:
    1) read python file method
    file python read &
      file_name = (eval(getenv("TOPDIR")//"/python_code/rm_files.py))
     
    2) run_python_code method
    !--- Run PYTHON code
    variable set variable_name=$_self.imp    integer_value=(eval(run_python_code("import glob, os, os.path")))
    variable set variable_name=$_self.dir    integer_value=(eval(run_python_code("dir = os.getcwd()")))
    variable set variable_name=$_self.files  integer_value=(eval(run_python_code("filelist = glob.glob(os.path.join(dir, 'tmp_*'))")))
    variable set variable_name=$_self.frmv   integer_value=(eval(run_python_code("junk = [os.remove(f) for f in filelist]")))
     Thanks for your help. 
    It is quite unfortunate that there is a limitation for single line python commads.
     
    Is MSC working on an update to run more sophisticated multiline python codes?
     
    Best regards,
    Ondrej
Reply
  • Hi Kent,
    thanks a lot for your answer it helped a lot.
    Both your ways are working:
    1) read python file method
    file python read &
      file_name = (eval(getenv("TOPDIR")//"/python_code/rm_files.py))
     
    2) run_python_code method
    !--- Run PYTHON code
    variable set variable_name=$_self.imp    integer_value=(eval(run_python_code("import glob, os, os.path")))
    variable set variable_name=$_self.dir    integer_value=(eval(run_python_code("dir = os.getcwd()")))
    variable set variable_name=$_self.files  integer_value=(eval(run_python_code("filelist = glob.glob(os.path.join(dir, 'tmp_*'))")))
    variable set variable_name=$_self.frmv   integer_value=(eval(run_python_code("junk = [os.remove(f) for f in filelist]")))
     Thanks for your help. 
    It is quite unfortunate that there is a limitation for single line python commads.
     
    Is MSC working on an update to run more sophisticated multiline python codes?
     
    Best regards,
    Ondrej
Children
No Data