• python --文件操作模式详解


    #f= open(r"aaa/a.txt",mode='rt') # f的值是一种变量,占用的是应用程序的内存空间,此时牵扯的是两个方面的资源
    #print(f)

    ##2.操作文件:读/写文件,应用程序对文件的读写请求都是向操作系统调用,然后由操作系统控制把硬盘把输入读入内存,或者写入硬盘
    #res=f.read()
    #print(res)

    #关闭文件 z
    #f.close()"""回收操作系统资源,f 还存在"""
    #f变量还存在,但是不能再读了
    #del f """回收应用程序程序资源"""


    #文件对象又称为文件句柄
    # with open("a.txt",mode='rt') as f1:
    # res=f1.read()
    # print(res)

    # with open("a.txt",mode='rt') as f1,open("b.txt",mode='rt') as f2:
    # res1=f1.read()
    # res2=f2.read()
    # print(res1)
    # print(res2)

    """指定字符编码"""
    """t 文本(默认的模式)
    1。 读写都是以str (unicode)为单位的
    2。文本文件
    3。必须指定encoding='utf-8'"""

    # 没有指定encoding 参数操作系统会使用自己的默认的编码,
    # Linux 和mac 默认使用utf-8
    # Windows默认gbk
    # with open ('c.txt',mode='rt',encoding='utf-8') as f:
    # res=f.read() #t模式会将f.read()读出来的结果解码成unicode
    # print(res,type(res))
    #
    #内存:utf-8格式的二进制----解码--->unicode
    #硬盘(c.txt 内容 utf-8格式的二进制)

    #以t 模式为基础进行内存操作
    ##1.rt

    在本python文件的目录下创建一个name.txt的文本文件,
    izhan:1005

    inp_username = input("please input your name:").strip()
    inp_password = input("please input your password:").strip()
     with open("name.txt",mode='rt',encoding='utf-8') as f:
         res=f.read()
         print(res)
         username,password=res.split(":")
         print(username)
         print(password)
     print(inp_username)
     print(inp_password)
     if inp_username == username and inp_password == password:
         print("congratulations! you can login")
     else:
         print("sorry! the password is wrong or has no this user")
    please input your name:izhan
    please input your password:1005
    izhan
    1005
    izhan
    1005
    congratulations! you can login
    
    随便输入看看:
    please input your name:dandan
    please input your password:0711
    izhan
    1005
    dandan
    0711
    sorry! the password is wrong or has no this user
    
    Process finished with exit code 0

    再创建一个name.txt 文件

    izhan:1005
    lili:1111
    dandan:0711
    另外:python spit 的学习:

    https://www.cnblogs.com/clairedandan/p/10926173.html

    inp_username = input("please input your name:").strip()
    inp_password = input("please input your password:").strip()
    
    with open("name.txt",mode='rt',encoding='utf-8') as f:
        for line in f:
            print(line,end='') # 没有end='',结果就会一行一行中间有一行空的。
            username,password=line.strip().split(":")
            # print(username)
            # print(password)
            if inp_username == username and inp_password == password:
                print("login successfully")
                break
        else:
            print("账号错误")
    please input your name:lili
    please input your password:0711
    izhan:1005
    lili:1111
    dandan:0711账号错误
    
    Process finished with exit code 0
    
    please input your name:izhan
    please input your password:1005
    izhan:1005
    login successfully
    
    Process finished with exit code 0
    
    """w:只写模式,当文件不存在时会创造空文件,当文件存在时会清空文件,"""
    # with open("d.txt",mode="wt",encoding="utf-8") as f:
    # # f.read()"""报错,不可读"""
    # f.write("hahaha ")
    # f.write("hello ")
    # f.write("wwwwww ")
    ##如果重新打开文件,则会清空之前的内容,如果连续write,则会连续写的

    """3.a 只追加写,"""
    # with open("a.txt",mode="at",encoding="utf-8") as f:
    # # f.read() 不能读
    # f.write("hahahahah ")
    # f.write("enenenenen ")
    # f.write("oooooo ")

    """a 重新打开文件,不会清空文件内容,会将文件指针直接移动到末尾
    a 文件一直用作记录日志,注册功能"""
    """a 模式注册功能"""
    # name=input("please input your name:")
    # pwd=input("please input your password:")
    # with open("db.txt",mode="at",encoding="utf-8") as f:
    # f.write("{}:{} ".format(name,pwd))

    w 可以用做文本文件的copy

    with open("e.txt",mode="rt",encoding="utf=8")as f1,
        open("f.txt",mode="wt",encoding="utf-8")as f2:
        res=f1.read()
        f2.write(res)
    src_file=input("源文件文件:")
    dst_file=input("目标文件:")
    with open(r"{}".format(src_file),mode="rt",encoding="utf=8")as f1,
        open(r"{}".format(dst_file),mode="wt",encoding="utf-8")as f2:
        res=f1.read()
        f2.write(res)
    /usr/local/bin/python3.8 /Users/futantan/PycharmProjects/S14/文件知识/文件处理.py
    源文件文件:/Users/futantan/PycharmProjects/S14/文件知识/a.txt
    目标文件:/Users/futantan/PycharmProjects/S14/a_copy.txt
    
    Process finished with exit code 0
    

     此时就可以发现有一个copy的文件了

    
    
  • 相关阅读:
    编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
    Spring依赖注入
    Spring的DI(Ioc)
    Spring的DI(Ioc)
    Spring的DI(Ioc)
    Spring管理bean的生命周期
    Spring管理Bean的三种创建方式
    简单模拟Spring管理Bean对象
    编写spring配置文件时,不能出现帮助信息
    Spring简介
  • 原文地址:https://www.cnblogs.com/clairedandan/p/14105609.html
Copyright © 2020-2023  润新知