python学习(一)

     应该是第多次学习python了,都是学习到一半,就停止了,这次要坚持下去,fighting!!!

函数学习:

        raw_input()   从标准输入读取一个字符串并自动删除尾的换行符,并将读取到的数据赋值给指定变量

例子:

>>> num = raw_input('Enter login name')

Enter login name

>>> num = raw_input('Now enter a number:')

Now enter a number:1024

>>> print 'Doubling your number:%d' % (int(num) *2)

 

Doubling your number:2048

        help()  的参数能得到相应的帮助信息

例子:

Help on built-in function raw_input in module __builtin__:

 

raw_input(...)

    raw_input([prompt]) -> string

    

    Read a string from standard input.  The trailing newline is stripped.

    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.

    On Unix, GNU readline is used if enabled.  The prompt string, if given,

    is printed without a trailing newline before reading.

实现命令的小程序

import sys

def readfile(filename):  

    '''Print a file to the standard output.'''

    f = file(filename)

    while True:

        line = f.readline()

        if len(line) == 0:

            break

        print line, 

    f.close()

# Script starts from here

if len(sys.argv) < 2:

    print 'No action specified.'

    sys.exit()

if sys.argv[1].startswith('--'):

    option = sys.argv[1][2:]

    # fetch sys.argv[1] but without the first two characters

    if option == 'version':  

        print 'Version 1.2'

    elif option == 'help':  

        print '''"

This program prints files to the standard output.

Any number of files can be specified.

Options include:

  --version : Prints the version number

  --help    : Display this help'''

    else:

        print 'Unknown option.'

    sys.exit()

else:

    for filename in sys.argv[1:]: 

        readfile(filename)

********************************************************************************************************************************

该程序能实现‘versioon“、”help“和读文件功能

命令:

 [@zw_68_66 pythontest]# python new.py a.txt

hello word!

[@zw_68_66 pythontest]# python new.py --version

Version 1.2

[@zw_68_66 pythontest]# python new.py --help

"

This program prints files to the standard output.

Any number of files can be specified.

Options include:

  --version : Prints the version number

  --help    : Display this help

*************************************************************************************************************************************

 

 

猜你喜欢

转载自km-moon11.iteye.com/blog/1953552