• python基础之文件操作


    文件操作:
    r,只能读。 【**】
    w,只能写,写之前清空。 【**】
    a,只能追加。【*】
    r+ 读:默认从0的光标开始读,也可以通过 seek 调整光标的为位置。
       写:从光标所在的位置开始写,也可以通过 seek 调整光标的位置。
    w+ 读:默认光标永远在写入的最后或0,也可以通过 seek 调整光标的位置。
       写:先清空。
    a+ 读:默认光标在最后,也可以通过 seek 调整光标的位置。然后再去读取。
       写:永远写到最后。
     
     ######################## 读取:r,只读不能写 + 文件不存在报错 ##########################
    """
    # 打开文件
    file_object = open('D:python_codeday01w.txt',mode='r',encoding='utf-8') # r,read; w,write; a,append;
    # 读取内容
    content = file_object.read()
    print(content)
    # 关闭文件
    file_object.close()
     
     
    # ######################## 写入:w,只写不能读(先清空文件) + 文件不存在则新建 ##########################
    # 打开文件
    file_object = open('D:python_codeday01log1.txt',mode='w',encoding='utf-8') # r,read(只读); w,write(只写,先清空,一般用于新建文件); a,append;
    # 写内容
    file_object.write('sundy')
    # 关闭文件
    file_object.close()
     
     
     
    # ######################## 写入:a,只追加不能读 + 不存在则新建 ##########################
    """
    """
    # 打开文件
    file_object = open('logfffff.txt',mode='a',encoding='utf-8') # r,read(只读); w,write(只写,先清空,一般用于新建文件); a,append;
    # 写内容
    file_object.write('你好')
    # 关闭文件
    file_object.close()
    """
    # r+可读可写
    """
    读取
    写入:根据光标的位置,从当前光标位置开始进行写入操作(可能会将其他的文字覆盖)
    """
    """
    file_object = open('D:python_codeday01log.txt',mode='r+',encoding='utf-8')
    file_object.seek(3) # 调整光标的位置(2为字节数),默认为0位置
    file_object.write('sundy')
    """
    """
    # # 读取内容
    # content = file_object.read()
    # print(content)
    #
    # file_object.write('666')
    # 关闭文件
    file_object.close()
    """
    # 可读可写
    # 写入时会将文件清空,读取时需要调整光标
    """"
    file_object = open('D:python_codeday01log.txt',mode='w+',encoding='utf-8')
    data = file_object.read()
    print(data)
    file_object.write('alex')
    file_object.seek(0)
    data = file_object.read()
    print(data)
    file_object.close()
    """
    # 可读可写
    file_object = open('D:python_codeday01log.txt',mode='a+',encoding='utf-8') #a追加光标默认放在追加值之后,写永远追加在最后,读可以移动光标读
    file_object.seek(9)
    data = file_object.read()
    print(data)
    file_object.seek(0)
    file_object.write('666')
    file_object.close()
     
    功能:
    # ###################################### 读操作
    # file_object = open('log.txt',mode='r',encoding='utf-8')
    # 读取文件的所有内容到内存
    # data = file_object.read()
    # 从当前光标所在的位置向后读取文件两个字符,得到字符串
    # data = file_object.read(2)
    # 读取文件的所有内容到内存,并按照每一行进行分割到列表中。
    # data_list = file_object.readlines()
    # print(data_list)
    # 如果以后读取一个特别大的文件 (**********)
    # for line in file_object:
    #     line = line.strip()#去掉换行符
    #     print(line)
    # file_object.close()
    # ###################################### 写操作
    """
    file_object = open('D:python_codeday01log.txt',mode='w',encoding='utf-8')
    file_object.write('asdfadsfasdf ')  # 换行符
    file_object.write('asdfasdfasdfsadf')
    file_object.close()
    file_object = open('D:python_codeday01log.txt',mode='r',encoding='utf-8')
    data = file_object.read()
    print(data)
    file_object.close()

    D:python3.6python3.6.exe C:/Users/yangjiayu/Desktop/老男孩IT2019Python开发课程/第7天课件/s21day07/5.读写操作-功能.py
    asdfadsfasdf
    asdfasdfasdfsadf

    练习题:

    # 练习1:请将user中的元素根据 _ 链接,并写入 'a1.txt' 的文件
    """
    user = ['alex','eric']
    data = "_".join(user)
    file_object = open('a1.txt',mode='w',encoding='utf-8')
    file_object.write(data)
    file_object.close()
    """
    # 练习2:请将user中的元素写入 'a1.txt' 的文件并以下形式
    """
    user = [
        {'name':'alex','pwd':'123'},    # alex|123
        {'name':'eric','pwd':'olbody'}, # eric|olbody
    ]
    file_object = open('a2.txt',mode='w',encoding='utf-8')
    for item in user:
        line = "%s|%s " %(item['name'],item['pwd'],)
        file_object.write(line)
    file_object.close()
    """
    # 练习3:请将a2.txt中的文件读取出来并添加到一个列表中 ['alex|123','eric|olbody']
    # 方式一

    file_obj = open('a2.txt',mode='r',encoding='utf-8')
    content = file_obj.read()
    file_obj.close()
    content = content.strip() #去掉两边空白或者
    data_list = content.split(' ')
    print(data_list)

    """
    result = []
    file_obj = open('a2.txt',mode='r',encoding='utf-8')
    for line in file_obj:
        line = line.strip()
        result.append(line)
    file_obj.close()
    print(result)
    """
    补充:
    1. 进制
       - 二进制 0b
       - 八进制
       - 十进制
       - 十六进制: x 
    2. 文件操作
       ```python
       # 打开文件
       f = open('要打开的文件路径',mode='r/w/a',encoding='文件原来写入时定义的编码')
      
       # 操作
       data = f.read()  # 读取文件的内容到内存(全部)
       f.write('要写入的内容')
      
       # 关闭文件
       f.close()
       ```
       ```python
       # 示例一 : 一般用于文字写入。
       f = open('a.txt',mode='w',encoding='utf-8')
       # a. 将 “你好” 根据encoding指定的编码转换成:
       #       “你好” --> 10001000 10001000 10001000 10001000 10001000 10001000
       # b. 将二进制写入到文件中。
       f.write('你好') # w打开文件,则write传入字符串
       f.close()
      
       # 示例二:一般用于图片/音频/视频/未知编码
       f = open('a.txt',mode='wb')
       # 1. 把要写入的字符串转换成二进制
       data = "我好困"
       content = data.encode('utf-8') # 将字符串按照utf-8编码转换成二进制
       # 2. 再将二进制写到文件中
       f.write(content) # wb打开文件,则write传入的是 二进制
       f.close()
      
       ```
     # 示例三:
       f = open('a.txt',mode='rb')
       # 1. 直接读取二进制数据
       data = f.read()
       f.close()
       print(data)
       文件操作
       - 模式
         - r /  w / a  【只读只写字符串】   *****
         - r+ / w+ / a+  【可读可写字符串】
         - rb / wb  /ab  【只读只写二进制】 *****
           - 字符串转换 “二进制”:
                v = "你好"
                data = v.encode('utf-8')
       
       D:python3.6python3.6.exe D:/python_code/day01/day8.py
    b'xe4xbdxa0xe5xa5xbd'
           - 二进制转换成字符串
                v = b'xe6x88x91xe5xa5xbdxe5x9bxb0'
                data = v.decode('utf-8')
         - r+b / w+b  / a+b 【可读可写二进制】
  • 相关阅读:
    Ruby自学笔记(二)— Ruby的一些基础知识
    Ruby自学笔记(一)— 基本概况
    Tomcat基础教程(四)
    Excel导入
    构建API
    序列化
    图片上传(练习)
    发邮件
    发短信
    Excel表导出
  • 原文地址:https://www.cnblogs.com/sundy08/p/11884951.html
Copyright © 2020-2023  润新知