I try to run a script in Python3 by exec() function.
I'm studying Python with the book 'Learning Python', O'Reilly 5th Edition. In the "CHAPTER 2 How Python Runs Programs" there is a method to like this:
>>> exec(open('script1.py').read())
This is my file script1.py
# A first script in python.
import sys
print(sys.platform)
print(2 **100)
x = 'Spam!'
print(x * 8)
input()
The expected output is:
win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
In the work I only can use WinXP :-(
But the real output in Python3 is:
>>> exec(open('script1.py').read())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
# A first script in python.
^
SyntaxError: invalid character in identifier
>>>
And the output in Python2 is:
>>> exec(open('script1.py').read())
win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 7, in <module>
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
>>>
I don't understand why that isn't working like the book says.