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.

1
  • Windows? Linux? cygwin? Gnome Terminal? Konsole? xterm? bash? sh? Commented Jan 19, 2015 at 23:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.