open python file in terminal code example
Example 1: run python file using python code
exec(open('file.py').read())
Example 2: python how to run terminal command
import os
os.system("ls -l")
Example 3: how to run python in terminal
$ python3 helloworld.py
$ python helloworld.py
Example 4: python if run in terminal
import sys
if sys.stdin.isatty():
print "running interactively"
else:
with open('output','w') as f:
f.write("running in the background!\n")
Example 5: to run python script in terminal i want to type python insted of python3
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3