Lets say that I have this code inside a JavaScript file:
var x = 10; x = 10 - 5; console.log(x); function greet() { console.log("Hello World!"); } greet()
How would I use Python to execute this code and "print"x
and Hello World!
?
Here is some pseudo code that further explains what I'm thinking:
# 1. open the script script = open("/path/to/js/files.js", "r") # 2. get the script content script_content = script.read() # 3. close the script file script.close() # 4. execute the script content and "print" "x" and "Hello World!" x = js.exec(script_content)
And, the expected result would look like this:
>>> 5 >>> "Hello World!"