1.  内置模块time
    time.time()     #获取当前时间戳。

    time.localtime()        #将时间戳转换为当前的时区的时间

    time.gmtime()          #将时间戳转换为UTC时区的时间

    >>> time.time()

    1505738117.122166

    >>> time.localtime(505738117.122166)

    time.struct_time(tm_year=1986, tm_mon=1, tm_mday=10, tm_hour=18, tm_min=48, tm_sec=37, tm_wday=4, tm_yday=10, tm_isdst=0)

    >>> time.gmtime(505738117.122166)

    time.struct_time(tm_year=1986, tm_mon=1, tm_mday=10, tm_hour=10, tm_min=48, tm_sec=37, tm_wday=4, tm_yday=10, tm_isdst=0)

    time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(505738117.122166))          #将元组格式的时间转换格式时间字符串

    >>> time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(505738117.122166))

     '1986-01-10 18:48:37'

    >>> time.strftime("%Y-%m-%d %H:%M:%S")

    '2017-09-18 20:40:36'
    
    time.strptime('1986-01-10 18:48:37',"%Y-%m-%d %H:%M:%S")                  #将格式时间按照格式转换为元组格式

    >>> time.strptime('1986-01-10 18:48:37',"%Y-%m-%d %H:%M:%S")

    time.struct_time(tm_year=1986, tm_mon=1, tm_mday=10, tm_hour=18, tm_min=48, tm_sec=37, tm_wday=4, tm_yday=10, tm_isdst=-1)

    >>> time.asctime(time.localtime())                #接收时间元组
    'Mon Sep 18 20:56:11 2017'
    >>> time.ctime(time.time())                         #接收的是时间戳
    'Mon Sep 18 20:54:56 2017'

    >>> help(time.strftime)
    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.


2.  内置模块datetime
   datetime.datetime.now()                            获取当前时间
      datetime.datetime(2017, 9, 18, 20, 58, 29, 153874)

      >>> datetime.datetime.now() + datetime.timedelta(3)            当前时间+3天
      datetime.datetime(2017, 9, 21, 21, 1, 7, 6000)        
      >>> datetime.datetime.now() + datetime.timedelta(-3)           当前时间-3天
      datetime.datetime(2017, 9, 15, 21, 1, 20, 441752)
      >>> datetime.datetime.now() + datetime.timedelta(hours=-3)     当前时间-3小时
      datetime.datetime(2017, 9, 18, 18, 1, 56, 963678)
      >>> datetime.datetime.now() + datetime.timedelta(hours=3)
      datetime.datetime(2017, 9, 19, 0, 1, 59, 555986)
   >>> datetime.datetime.now() + datetime.timedelta(minutes=3)
   datetime.datetime(2017, 9, 18, 21, 5, 10, 591532)
   >>> datetime.datetime.now() + datetime.timedelta(minutes=-3)
      datetime.datetime(2017, 9, 18, 20, 59, 13, 33811)
      >>>
    
    
3.    rangdom模块
    
    >>> random.random()                          (0,)从0到1之间的数
    0.7161203610306999
    >>> random.randint(1,3)                       [1,3]  从1到3之间的数,包括1,3
    1
    >>> random.randint(1,3)
    2
    >>> random.randint(1,3)
    3
    >>>
    >>> random.randrange(3)                    [0,3)从0到3之间的数,不包括3
    1
    >>
    >>> random.choice('brace')                从一个字符或一个列表等中随机取一个元素
    'r'
    >>> random.choice([1,2,3])
    3
    
    >>> random.sample('brace',3)        从一个字符或一个列表等中随机取指定个数的随机元素
    ['a', 'c', 'r']
    >>>
    >>> random.sample(('a','b','c'),2)
    ['c', 'b']
    
    >>> random.uniform(1,3)                就是在random.random()的基础上指定区间
    2.013822370201594
    >>>
    >>> random.uniform(1,3)
    2.836095958742261
    
    >>> l = [1,2,3,4,5,6,7]
    >>> random.shuffle(l)                随机将列表顺序打乱
    >>> l
    [1, 4, 6, 7, 2, 3, 5]

    
4. string模块:
    >>> string.ascii_letters
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>> string.ascii_lowercase
    'abcdefghijklmnopqrstuvwxyz'
    >>> string.ascii_uppercase
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>>
    
    ord                将字符转换为asciii
    chr转换            将asciii转化为字符
    A-Z                Asci是从65-90
    
    
案例:
    随机4位验证码:
    
    def randcode(n):
        strtmp = ""
        for i in range(n):
            current_num = random.randrange(0,n)
            if current_num == i:
                tmp = chr(random.randint(65,90))
            else:
                tmp = str(random.randint(0,9))
            strtmp += tmp
        return strtmp
    
    >>> randcode(6)
    'KNJ104'
    >>> randcode(6)
    'E3745E'
    >>> randcode(6)
    'GH34QR'
    >>> randcode(6)
    'W93J36'
    >>> randcode(6)
    'YK519W'
    >>> randcode(4)
    'UXA9'
    >>> randcode(10)
    '17OA3BMG20'
    >>>

    
5.    os模块:
    
    >>> os.getcwd()
    '/python'
    >>>
    >>> os.chdir("/etc")
    windows
    >>> os.chdir("C:\\system")            需要转义一下
    >>>
    或者
    >>> os.chdir(r"C:\system")
    >>>
    >>> os.getcwd()
    '/etc'
    >>>
    >>> os.curdir                    表示当前目录
    '.'
    >>> os.pardir                    表示上一层目录
    '..'
    os.makedirs("a/b/c/d")            递归的创建目录
    os.mkdir('xxx')                    只能创建一级目录,不能递归创建
    
    os.removedirs("a/b/c/d")        用来递归清除空目录
    os.rmdir("xxx")                    删除当前目录xxx
    
    os.listdir()                    列出当前文件及文件夹
    
    os.remove()                        删除一个n文件
    os.rename("oldname","newname")    修改文件名
    
    os.stat()
    >>> os.stat('process.py')        查看文件属性
    os.stat_result(st_mode=33276, st_ino=401815, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=116, st_atime=1505051764, st_mtime=1505051762, st_ctime=1505051762)
    
    
    >>> os.sep                路径分隔符
    '/'
    windows
    >>> os.sep
    '\\'
    
    
    >>> os.linesep            换行符
    '\n'
    windows
    >>> os.linesep
    '\r\n'
    
    
    >>> os.pathsep            //系统环境变量的分割符
    ':'
    os.environ                //查看系统环境变量
    
    os.name                    //获取平台名:
    
    linux = > 'posix'
    window => 'nt'
    
    os.system('ls')            执行系统命令
    
    os.path.abspath()        获取某个文件的绝对路径。
    
    os.path.split()            将文件和目录分割开
    >>> os.path.split("/ss.s/sssd/ss.txt")
    ('/ss.s/sssd', 'ss.txt')
    
    os.path.dirname(path)   返回path的目录
    os.path.basename        返回path文件名
    >>> os.path.basename("/sss/sss/ss/11/1212")
    '1212'
    
    os.path.exists()        判断是否存在
    
    
    >>> os.path.isabs("/etc/")
    True
    >>>
    >>> os.path.isabs("etc/")            判断开头是不是从根开头
    False
    
    os.path.isfile()        判断是否文件
    os.path.isdir()            判断是否是目录
    
    os.path.jion(path1,path2,....)        将多个路径组合返回
    
    
    os.path.getatime()            获取访问时间
    os.path.getmtime()            获取修改时间
    
sys 模块:

    sys.argv                      获取参数列表
    sys.exit(n)                   退出程序,正常退出时exit(0)t
    sys.maxint                   最大int值
    sys.path                      返回python初始化环境变量
    sys.platform                返回平台名称
    sys.stdout.write("please")
    val = sys.stdin.readline()[:1]
    
    




    
    
   

posted on 2017-09-19 21:32  仙寓游子  阅读(184)  评论(0)    收藏  举报