在终端内打开终端时执行python脚本

时间:2015-01-19 23:39:29

标签: python terminal

我感兴趣的是,当你打开一个终端窗口时,有一种方法可以运行python脚本。例如

print "hello world"

每次打开终端时,都会出现hello world。

2 个答案:

答案 0 :(得分:2)

如果您使用的是bash,那么当您打开终端时,您在〜/ .bashrc文件中的任何内容都会被运行,即

python my_script.py

将执行脚本my_script.py。

答案 1 :(得分:0)

  

每次打开终端时,都会出现hello world。

只是做:

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

你的任何python脚本中的任何地方。

要达到此效果,您必须执行以下两项操作

1 - 为了便于携带,您必须制作一个小模块,如下所示 -

# 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 - 现在位于~/.bashrc

export PYTHONSTARTUP=$HOME/.pythonstartup

并将您的python代码放在​​$ HOME / .pythonstartup中,例如:

from goodManners import clrscr
相关问题