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

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-

    #文件操作

    a = open("xxx.txt",encoding="utf-8").read() #打开,编码格式,读取文件内容#默认GPK编码

    # r 读 只能读不能写
    a = open("xxx.txt",'r',encoding="utf-8") #文件句柄 文件名 读r(写w) 编码 r读,只能读
    c1=a.read() #读到内容,光标到结尾
    c2=a.read() #读不到内容
    print(c1,c2) #c2是空的
    a.close() #关闭文件

    # w 写 只能写,不能读,会删以前内容
    a=open("qq.txt",'w',encoding="utf-8") # 文件名,写,编码 以前的内容会删除
    a.write("你好 ") #写入文件hello内容 换行
    a.write("我好 ")
    a.close() #关闭文件

    #a 追加内容 不删以前内容 不能读
    a=open("qq.txt",'a',encoding="utf-8") # 文件名,追加,编码 不会删以前内容 不能读
    a.write("他好 ") #写入文件hello内容 换行
    a.write("大家好")
    a.close() #关闭文件

    print('++++++')

    #读操作 读汉字有问题
    ''' 只能读小文件 比较渣
    a = open("xxx.txt",'r',encoding="utf-8") #文件句柄 文件名 读r(写w) 编码 r读,只能读
    #print(a.readline()) #只读一行 当前行 第一行
    #for i in range(5): #只读前5行
    # print(a.readline())

    #print(a.readlines()) #显示成列表
    for i in a.readlines(): #显示所有内容
    # .readlins()只能读小文件 不能处理大文件 先把硬盘文件内容,放入内存。
    print(i.strip()) #.strip() 去掉空格和换行
    a.close()

    print('+++++++')
    a = open("xxx.txt",'r',encoding="utf-8") #文件句柄 文件名 读r(写w) 编码 r读,只能读
    for index,i in enumerate(a.readlines()): #取出下标
    if index == 1: #第二行下杯是1, 时显示- - - - -
    print('- - - - - -')
    continue
    print(i.strip()) #.strip() 去掉空格和换行
    a.close()
    '''
    #正常读
    c=0
    a = open("xxx.txt",'r',encoding="utf-8") #文件句柄 文件名 读r(写w) 编码 r读,只能读
    for i in a:
    if c == 1 : #c+到1时, 显示- - - - -
    print('- - - - - - -')
    c += 1
    continue
    print(i.strip()) #..strip() 去掉空格和换行
    c+=1
    a.close()

    print('+++')

    a = open("xxx.txt",'r',encoding="utf-8") #文件句柄 文件名 读r(写w) 编码 r读,只能读
    print(a.tell()) #查看位置
    print(a.readline(5)) #读5个字符
    print(a.readline())
    print(a.tell())
    a.seek(0) #回到0首位置
    print(a.readline())

    print(a.encoding) #显示文件编码
    print(a.fileno()) #文件编号 用不到

    #print(a.flush()) #写入模式,强制写入硬盘 默认先放内存,放到一定量,再一起输出

    print('- - - - - - - - - - - -')
    '''
    #进度条
    import sys,time #标题输出模块,时间模块
    sys.stdout.write("#") #输出# 类似print
    for i in range(20): #输出1-20
    sys.stdout.write("#") #输出#
    sys.stdout.flush() #强制输出 默认先放内存,放到一定量,再一起输出
    time.sleep(0.1) #间隔0.1秒
    '''
    #help(open) #帮助
    '''
    print(' + + + ')
    a = open("qq.txt",'a',encoding="utf-8") #文件句柄 文件名 读r(写w) 编码 r读,只能读
    a.seek(3) #跳到第3个字符的位置
    a.truncate(5) #截取前3个字符
    a.close()
    '''
    #r+ 读写
    a=open('qq.txt','r+',encoding='utf-8')
    print(a.readline())
    print(a.tell())
    a.write('++++++')
    a.close()

    #w+ 写读 先创建文件,
    print('- - - - - - - - - - - - - - - - - - - - - - - - - - - -')
    a=open('qqq.txt','w+',encoding='utf-8')
    a.write("************** ")
    a.write("************** ")
    print(a.tell())
    a.seek(10)
    print(a.tell())
    print(a.readline())
    a.write('+++') #不能在10的位置号,追加到最后

    ''' 文件模式
    r 只能读不能写
    r+ 读写 打开读,追加 #用最多#
    w 创建新文件
    w+ 写读 卵用
    a 追加内容 不删以前内容 不能读
    a+ 追加读
    rb 二进制编码 视频是二进制文件
    #网络传输(只能用二进制)
    wb 写二进制编码文件
    .write("hello ".encode()) #encode()转成二进制
    '''
  • 相关阅读:
    在o(N log N)时间内使用恒定的空间复杂度对链表进行排序
    归并排序
    The method of type must override a superclass method解决方式
    android sdk design Iconography
    android sdk design Typography
    android sdk design Metrics and Grids
    android模拟器被点击时总是自动输入C的解决
    “System.Data.Entity.ModelConfiguration.ModelValidationException”类型的未经处理的异常在 EntityFramework.dll 中发生
    js中的this使用
    MVC身份认证 解决办法 测试可行
  • 原文地址:https://www.cnblogs.com/pojue/p/7881721.html
Copyright © 2011-2022 走看看