zoukankan      html  css  js  c++  java
  • 文件操作

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    # 1.open()打开文件路径,后面跟 encoding解码。不指定解码的话,默认的是系统的编码 默认的是读r
    # file1=open("测试",encoding="utf-8")
    # data=file1.read()
    # print(data)
    # file2=open("测试","r",encoding="utf-8")
    # data=file2.read()
    # print(data)
    # file2.close()
    # # 2.w是open方法里的写属性。
    # data2=open("测试1","w",encoding="utf-8")
    # data2.write("来啦利来 来来拉里拉里 来阿里rrrrrrrr阿里阿拉")
    # data2.close()
    # 3.a是在最后追加。
    # data=open("测试","a",encoding="utf-8")
    # file2=data.write("我是新追加的内容 ,我是第二行追加 ")
    # data.close()
    # 4.open其它的用法wr + r+ a+
    # file=open("测试1","r+",encoding="utf-8")
    # data=file.read()
    # print(data)
    # file.write("新de内容 ")
    # file.close()
    # 5.以b的方式打开。
    # open("f:123.txt","rb",encoding="utf-8")#以b方式打开的或编辑的不能指定编码。
    # data1=open("测试1","rb")
    # shuju=data1.read()
    # print(shuju.decode("utf-8"))#文件保存的时候用的是encoding 而当我们读取的时候就
    # # 是解码decode,这个一定要注意。是编码还是解码。
    # data1.close()
    # f=open("test","wb")#以b方式打开的或编辑的不能指定编码。
    # f.write("战三里斯五四".encode("utf-8"))
    # f.close()
    # 6 .closed()判断文件是否关闭,readline 读取文件的一行,readlines读取文件全部 flush刷新
    # 保存到硬盘上 tell读取光标的当前位置,按字节来读取。newline=定义不同平台默认回车显示。gbk1
    # 个中文3个字节,windows中一个回车是包含着 两个字符的。seek移动光标,
    # f=open("测试1","rb")
    # print(f.tell())
    # print(f.seek(6,0))
    # data=f.readlines()
    # # print(type(data))
    # for d in data:
    # print(d.decode("utf-8"))
    # f.close()
    # 7.读取大文件操作
    f=open("测试1","rb")
    pyl = -6
    date = f.readlines()
    while len(date)>1:
    f.seek(pyl, 2)
    print("已经是最后一行了,内容是:", date[-1].decode("utf-8"))
    break
    else:
    pyl*=2

  • 相关阅读:
    oracle 数据库服务名怎么查
    vmware vsphere 6.5
    vSphere虚拟化之ESXi的安装及部署
    ArcMap中无法添加ArcGIS Online底图的诊断方法
    ArcGIS中字段计算器(高级计算VBScript、Python)
    Bad habits : Putting NOLOCK everywhere
    Understanding the Impact of NOLOCK and WITH NOLOCK Table Hints in SQL Server
    with(nolock) or (nolock)
    What is “with (nolock)” in SQL Server?
    Changing SQL Server Collation After Installation
  • 原文地址:https://www.cnblogs.com/Centwei/p/10261985.html
Copyright © 2011-2022 走看看