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
  • Another workaround with four additional lines would be to dump the desired content into an intermediate python file and simply read/cleanup after creation.
     
    file text open file="test.py" open_mode = overwrite
    file text write format="import glob, os, os.path"
    file text write format="cwd = os.getcwd()"
    file text write format="filelist = glob.glob(os.path.join(cwd, 'tmp_*'))"
    file text write format="for f in filelist:"
    file text write format="  os.remove(f)"
    file text close file="test.py"
    file python read file = "test.py"
    var set var = DeleteIt int = (eval(REMOVE_FILE("test.py"))
     
    It's still using a python file, but saves the administrative work of having files installed & found on the target machine (especially TOPDIR is one of the top-forbidden zones in our IT)
Reply
  • Another workaround with four additional lines would be to dump the desired content into an intermediate python file and simply read/cleanup after creation.
     
    file text open file="test.py" open_mode = overwrite
    file text write format="import glob, os, os.path"
    file text write format="cwd = os.getcwd()"
    file text write format="filelist = glob.glob(os.path.join(cwd, 'tmp_*'))"
    file text write format="for f in filelist:"
    file text write format="  os.remove(f)"
    file text close file="test.py"
    file python read file = "test.py"
    var set var = DeleteIt int = (eval(REMOVE_FILE("test.py"))
     
    It's still using a python file, but saves the administrative work of having files installed & found on the target machine (especially TOPDIR is one of the top-forbidden zones in our IT)
Children
No Data