zoukankan      html  css  js  c++  java
  • time模块与os模块常用方法

    import模块:

    s1=time.localtime() 结构化时间 #中国,东巴区 将时间戳------》结构化时间
    s2=time.gmtime() 结构化时间 #UTC时间

    t='2016-06-21'

    res=time.mektime(time.localtime()) #将结构化时间----------》》时间戳
    print(res)
    res2=time.strftime('%Y-%m-%d',time.loocaltime()) #将结构化时间----》》字符串时间
    print(res1)
    res3=timt.strftime('2017:06:21','%Y:%m:%d') #字符串时间--------》》结构化时间
    print(res3
    -----------------------------------------------》》》
    >>> import random
    >>> random.random() # 大于0且小于1之间的小数
    0.7664338663654585
    >>> random.randint(1,5) # 大于等于1且小于等于5之间的整数
    >>> random.randrange(1,3) # 大于等于1且小于3之间的整数
    >>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5]
    >>> random.sample([1,'23',[4,5]],2) # #列表元素任意2个组合
    >>> random.uniform(1,3) #大于1小于3的小数
    1.6270147180533838
    >>> item=[1,3,5,7,9]
    >>> random.shuffle(item) # 打乱次序
    >>> item
    [5, 1, 3, 7, 9]

    os模块:
    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
    os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
    os.makedirs('dirname1/dirname2') 可生成多层递归目录
    os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
    os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
    os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
    os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
    os.remove() 删除一个文件
    os.rename("oldname","newname") 重命名文件/目录
    os.stat('path/filename') 获取文件/目录信息
    os.path.abspath(path) 返回path规范化的绝对路径
    os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
    os.path.basename(path) 返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即
    os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
    os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
    os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
    os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
    os.path.getsize(path) 返回path的大小

  • 相关阅读:
    Codeforces Round #644 (Div. 3) A~G
    西安邮电大学第五届ACM-ICPC校赛(同步赛) B(拓扑排序)
    Codeforces Round #642 (Div. 3)A~D
    Codeforces Round #641 (Div. 2)A~D
    Codeforces Round #634 (Div. 3)A~E
    Educational Codeforces Round 85 (Rated for Div. 2)ABCD
    Codeforces Round #631 (Div. 2) ABD
    Codeforces Round #629 (Div. 3) E
    Educational Codeforces Round 84 (Rated for Div. 2) E
    yp训练赛3/21
  • 原文地址:https://www.cnblogs.com/sunxiansheng/p/7688539.html
Copyright © 2011-2022 走看看