0

Im interested if there is a way to run a python script when you open a terminal window. For example

print "hello world"

Every time i open a terminal, hello world would appear.

Eigenvalue
  • 1,093
  • 1
  • 14
  • 35

2 Answers2

2

If you are using bash, anything you put in your ~/.bashrc file will be run when you open the terminal, i.e.

python my_script.py

will execute the script my_script.py.

horns
  • 1,843
  • 1
  • 19
  • 26
  • Hi I did this then did . .bashrc and it executed my program in the window. However, i then opened another terminal and the program didnt run. – Eigenvalue Jan 20 '15 at 02:30
0

Every time i open a terminal, hello world would appear.

Just do:

clrscr("Hello World")                   # or whatever string you want

anywhere in any of your python scripts.

To achieve this effect, you have to do the following 2 things

1- For sake of portability you have to make a small module as shown below-

# goodManners.py
from os import system as command        # for calling to system's terminal
from platform import system as osName   # for getting the OS's name

def clrscr(text):
    if osName()=='Windows':
        command('cls')
    else:
        command('clear')
    print(text)

2- Now in your ~/.bashrc:

export PYTHONSTARTUP=$HOME/.pythonstartup

and put your python code in $HOME/.pythonstartup, like:

from goodManners import clrscr
RinkyPinku
  • 410
  • 3
  • 20