Eigenvalue
Eigenvalue

Reputation: 1133

Executing a python script when a terminal is opened within that terminal

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.

Upvotes: 0

Views: 289

Answers (2)

RinkyPinku
RinkyPinku

Reputation: 410

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

Upvotes: 0

horns
horns

Reputation: 1933

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.

Upvotes: 2

Related Questions