• 常用模块


    os模块

    import os
    #对操作系统的一些操作
    print(os.getcwd()) #取当前工作目录
    os.chmod("时间模块.py",2)#给文件/目录加权限,对windows的下面不好使1执行,2写,4读
    print(os.chdir("../day5"))  #更改当前目录
    print(os.makedirs("nhy/python")) #递归创建文件夹,父目录不存在时创建父目录
    print(os.mkdir("day6/huangrong")) #创建文件夹
    os.remove("test2")  #只能删除文件
    os.rmdir('test2')   #只能删除文件夹
    print(os.listdir('d:\'))  #列出目录下所有文件
    os.chdir('D:学习python自动化day6day6')
    os.rename("test","test2")   #将test重命名为test2
    print(os.sep) #当前操作系统的路径分割符
    print(os.linesep)#当前操作系统的换行符
    print(os.pathsep) #当前系统环境变量每个路径分隔符
    print(os.environ)  #当前系统环境变量
    print(os.name) #当前操作系统名称,Windows系统都是nt linux都是posix
    res=os.system('ipconfig') #执行操作系统命令的,但是获取不到结果
    res=os.popen('ipconfig').read() #可以获取到命令执行的结果
    print(res)
    print(os.path.abspath(__file__)) #获取绝对路径
    print(os.path.split("D:pythontestpythonday6常用模块.py"))#分割路径和文件名
    print(os.path.dirname("D:pythontestpythonday6常用模块.py"))#获取父目录,获取它的上一级目录
    print(os.path.basename("D:pythontestpythonday6常用模块.py"))#获取最后一级,如果是文件显示文件名,如果是目录显示目录名
    print(os.path.exists("D:pythontestpythonday6常用模块.py"))#目录/文件是否存在
    print(os.path.isabs("..day6"))#判断是否是绝对路径
    print(os.path.isfile("常用模块.py"))#判断是否是一个文件,1、文件要存在2、必须是一个文件
    print(os.path.isdir("D:pythontest"))#是否是一个路径,目录是否存在
    size=os.path.getsize("常用模块.py")#获取文件的大小,不能超过2m
    print(size)
    print(os.path.join("root",'mysql'))#拼接成一个路径
    for abs_path,dir,file in os.walk(r'D:pythontestpythonday6'):
        print(abs_path,dir,file)    # abs_path 当前循环的绝对路径dir 目录下面所有的文件夹 [ ]  file 目录下面的所有文件  []

    time,datetime模块

    import time
    import datetime
    
    x=time.localtime(12342344)  #传入一个时间戳
    # print(help(x))
    print(x)   #打印x(tuple)time.struct_time(tm_year=1970, tm_mon=5, tm_mday=24, tm_hour=4, tm_min=25, tm_sec=44, tm_wday=6, tm_yday=144, tm_isdst=0)
    print(x.tm_year)  #可以查看该时间戳是多少年1970
    print('是1970的day :%d'%x.tm_yday)  #是当年的第几天,是1970的day :144
    # strftime("格式",struct_time)--->"格式化字符串"
    # strptime("格式化字符串","格式")--->struct_time
    x=time.localtime()
    y=time.strftime("%Y-%m:%d %H:%M:%S",x)
    print(y)  #2018-04:26 10:05:35
    
    x=time.strptime("2018-04:26 10:05:35","%Y-%m:%d %H:%M:%S")
    print(x)#time.struct_time(tm_year=2018, tm_mon=4, tm_mday=26, tm_hour=10, tm_min=5, tm_sec=35, tm_wday=3, tm_yday=116, tm_isdst=-1)
    
    print(time.asctime())  #Thu Apr 26 11:04:26 2018
    print(time.ctime())#Fri May  4 16:56:32 2018
    
    print(datetime.datetime.now())#2018-05-04 16:56:32.118196
    y=datetime.datetime.now()+datetime.timedelta(3) #当前时间往后3天2018-05-07 16:56:32.118196
    print(y)
    z=datetime.datetime.now()+datetime.timedelta(hours=-3) #3个小时以前2018-05-04 13:56:32.118196
    print(z)

    random模块

    import random
    import string
    # print(random.randint(1,3))
    print(random.randint(0,99)) #随机整数
    print(random.randrange(0,101,2)) #随机0-100之间的偶数
    print(random.randrange(4)) #0-3随机取
    print(random.random()) #随机浮点数
    print(random.uniform(1,10))  #一定范围内的随机浮点数
    print(random.choice('12345'))  #随机取1个字符
    print(random.choice(['aaa','bbb','ccc','ddd']))  #随机取字符串
    #洗牌
    l=[1,2,3,4,5]
    random.shuffle(l)
    print(l)  #[2, 5, 1, 4, 3]

     

    hashlib模块

    import hashlib
    m=hashlib.md5()
    
    passwd = 'NHY_*&^_1982343532'
    # passwd.encode()#把字符串转成bytes类型
    m.update(passwd.encode())#不能直接对字符串加密,要先把字符串转成bytes类型
    print(m.hexdigest()) #md5加密是不可逆
    
    def my_md5(str):
        import hashlib
        new_str=str.encode() #把字符串转成bytes类型
        m=hashlib.md5()  #实例化md5对象
        m.update(new_str)
        return m.hexdigest
    print(my_md5('123qwsdewe'))
  • 相关阅读:
    JAVA自学笔记13
    非常不错的一款打字代码效果
    诡异的python文件
    Linux 虚拟机 docker 上 搭建 python 机器学习 平台
    git无法pull仓库: refusing to merge unrelated histories
    Python
    28 Jupyter Notebook tips, tricks, and shortcuts[Re-post]
    What is the best way to calculate a checksum for a file that is on my machine?
    Building MAPI Applications on 32-Bit and 64-Bit Platforms
    Linux 查看进程 关闭进程
  • 原文地址:https://www.cnblogs.com/xiaojing2017/p/8991552.html
Copyright © 2020-2023  润新知