Python脚本

一 Python脚本

何时使用Python编写脚本:

#-If you find you need to use arrays for anything more than assignment of ${PIPESTATUS}, you should use Python.
#-If you are writing a script that is more than 100 lines long, you should probably be writing it in Python instead. Bear in mind 
that scripts
grow. Rewrite your script in another language early to avoid a time-consuming rewrite at a later date. 如果您发现您需要使用数组来进行任何超过${PIPESTATUS}的赋值,那么您应该使用Python。 如果您正在编写一个长度超过100行的脚本,那么您可能应该用Python来编写它。记住,脚本会增长。尽早用另一种语言重写你的脚本,避免在以后的日子里花时间重写。

 

1.  创建 .py文件

[root@localhost tmp]# touch script.py

 

2. 编写

[root@localhost tmp]# vi script.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-

name = raw_input('What is name?:')
age = raw_input('Age is ?:')
print "username:{},Age:{}".format(name,age)

#注意:Linux默认版本是py2,应该使用 raw_input,不应是 input。(不然会报错:NameError: name 'xxx' is not defined)

 

3.赋予脚本执行权限

    

 

 4. 执行

    

 

 

二  Py两种用途

一. 脚本:一个文件就是整个执行程序,用来被执行

二. 模块:文件中存放这一堆功能,用来被传入执行

 

 

三 __name__和__mian__

 

#当文件被当做脚本执行时:__name__ 等于___main__


#当文件被当做模块导入时:__name__ 等于 模块名

 

posted @ 2018-06-30 21:21  shadow3  阅读(193)  评论(0)    收藏  举报