• Python模块


    模块

    python中以.py结尾的文件叫模块

    模块的分类

    导包

    当跨模块调用函数

    • 第一种方式
      导入:import 模块名
      调用:模块名.函数名

    注意:在导入模块时,会执行这个模块中的所有的调用的脚本。
    如果想调用的脚本调入后不执行那么可以使用main方法

    • 第二种方式
      导入:from 模块名 import 函数名
      调用:函数名()

    注意:当跨包调用时那么导入需要加上包名:from 包名.模块名 import 函数名

    pycharm的快捷键

    • alt+Enter
    • ctrl+alt+空格

    补充:可以使用print(sys.path)查看导入模块时的搜索的顺序和路径

    文件操作(File)

    open() 打开文件
    file.close() 关闭文件
    file.read() 读取文件
    file.write(str) 将字符串写入文件

    # 打开文件
    file=open("E:\a.txt","r+")
    #file.write("我给你一个久久的望着孤月的人的悲哀——博尔赫斯")
    #print(file.read())
    print(file.readline())
    file.close()
    

    文件属性

    file.closed 判断文件是否关闭,关闭则为True
    file.mode 返回被打开文件的访问模式
    file.name 返回文件的路径
    file.encoding 返回文件编码

    mode参数

    模式 描述
    r 以只读方式打开文件。
    rb 以二进制格式打开一个文件用于只读。一般用于非文本文件如图片等。
    r+ 打开一个文件用于读写。
    rb+ 以二进制格式打开一个文件用于读写。一般用于非文本文件如图片等。
    w 打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果文件不存在,创建新文件。
    a 打开一个文件用于追加。新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。

    os模块

    对系统的文件夹和文件进行操作

    os.mkdir("E:aaa") 创建文件夹
    os.makedirs("E:bbccc") 创建多层级文件夹
    os.path.isdir("E:aaa") 判断一个文件夹是否已存在
    os.path.isfile("E:aaa") 判断一个文件是否已存在
    os.rename("E:aaa","E:bb") 重命名
    os.rmdir("E:aaa") 删除文件夹
    os.remove("E:a.txt") 删除文件,不能删除文件夹
    print(os.getcwd) 获得当前的工作目录

    time模块

    1、时间格式

    1.时间戳:从格林威治时间:1970.1.1开始到现在的总秒数。print(time.time())
    2.时间元组:print(time.localtime())
    3.英文时间字符串:print(time.asctime())

    2、时间格式相互转换

    1.把时间戳转换成时间元组:print(time.localtime(157431333))
    2.把时间元组转换成时间戳:time.mktime((2019,11,21,14,21,45,0,0,0))

    3.把时间元组转换成字符串:print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
    4.把字符串转换成时间元组:print(time.strptime("2019-11-21 14:24:30","%Y-%m-%d %H:%M:%S"))

    3、时间切割

    print(time.localtime().tm_year)
    print(time.localtime().tm_mon)
    print(time.localtime().tm_mday)
    print(time.localtime().tm_hour)
    print(time.localtime().tm_min)
    print(time.localtime().tm_sec)
    print(time.localtime().tm_wday)
    print(time.localtime().tm_yday)
    print(time.localtime().tm_isdst)

    4、睡眠

    time.sleep(3)

    datetime模块

    1、获得datetime对象

    dt=datetime.datetime(2019,11,21,15,25,35,10)
    print(dt,type(dt))
    dt=datetime.datetime.today()    #获得当前系统时间
    print(dt,type(dt))
    dt=datetime.datetime.now()    #获得当前系统时间
    print(dt,type(dt))
    

    2、时间格式互相转换

    1.把datetime转换成时间戳:print(dt.timestamp())
    2.把时间戳转换成datetime:print(dt.fromtimestamp(dt.timestamp()))

    3.把datetime转换成字符串:print(dt.strftime("%Y-%m-%d %H:%M:%S.%f"))
    4.把字符串转换成datetime:print(dt.strptime("2019-11-21 15:33:20.100","%Y-%m-%d %H:%M:%S.%f"))

    3、切割

    print(dt.year)
    print(dt.month)
    print(dt.day)
    print(dt.hour)
    print(dt.minute)
    print(dt.second)
    print(dt.microsecond)

    异常处理

    try:
        file=open("E:\a.txt","r+")
    except FileNotFoundError as e:    # 捕捉“文件没有找到”错误
        print("文件没有找到")
    

    结果:

    try:
        a=10/0
    except ZeroDivsionError as e:    #捕捉“除数等于0”错误
        print("除数等于0")
    

    结果:

    file = open("E:\a.txt", "r+")  #前提:在E盘有一个文件a.txt,以读写方式打开
    try:
        a=10/0
        print("aaaa")   #若上一步报异常,该语句就不会执行,若必须执行,就将语句放在finally中
    except Exception as e:  #Exception可以捕获python的所有异常,异常提示信息保存在变量e中
        print(e)    #在开发中会打印异常提示信息
        print("系统错误")
    finally:    #不管有无异常都会执行,一般执行关闭文件、关闭通道语句
        file.close()   #关闭文件流,否则会占内存
  • 相关阅读:
    CodeSmith将模板文件批量生成文件的方法
    VSFTP
    Retrieving the COM class factory for component with CLSID … failed due to the following error: 80070005.
    简单的centos 5.3 LEMP以及vsftpd配置
    jQuerySelectors(选择器)的使用(一、基本篇)
    文件服务器 之 VSFTPD的高手篇
    jQuery常用技巧大放送
    google ads 黑名单目录
    “VPS FTP应用”目录存档
    用上了LNMP,一键安装实在爽,速度杠杠的 用上了LNMP,一键安装实在爽,速度杠杠的
  • 原文地址:https://www.cnblogs.com/TD1900/p/11903830.html
Copyright © 2020-2023  润新知