zoukankan      html  css  js  c++  java
  • (Python基础)文件操作

    对文件操作流程

    1. 打开文件,得到文件句柄并赋值给一个变量
    2. 通过句柄对文件进行操作
    3. 关闭文件

    现有文件如下 命名为7 years

    Once I was seven years old my momma told me
    那年我七岁 妈妈就对我说
    Go make yourself some friends or you'll be lonely
    去交些朋友 不然你会孤独寂寞
    Once I was seven years old
    那年我七岁
    It was a big big world but we thought we were bigger
    这个辽阔的大千世界 总以为我们也会变得更加强大
    Pushing each other to the limits we were learning quicker
    将彼此逼到绝境 我们得以更快的成长
    By eleven smoking herb and drinking burning liquor
    十一岁那年 我吸大麻 喝烈性酒
    Never rich so we were out to make that steady figure
    生活捉襟见肘 我们离家奋斗只为有个稳定收入
    Once I was eleven years old my daddy told me
    十一岁那年 爸爸对我说
    Go get yourself a wife or you'll be lonely
    给自己找个妻子 否则你会空虚寂寞
    Once I was eleven years old
    那年我十一岁
    I always had that dream like my daddy before me
    我常梦想着有朝一日能像爸爸一样成为一个歌手
    So I started writing songs I started writing stories
    所以我开始写歌 开始写不同的故事
    Something about the glory just always seemed to bore me
    曾经的光辉岁月 对我来说也已厌倦
    Cause only those I really love will ever really know me
    因为只有那些我真正爱的人才真的懂我
    Once I was 20 years old my story got told
    二十岁那年 我的故事广为流传
    Before the morning sun when life was lonely
    黎明还未照耀前 孤独的我无人相伴
    Once I was 20 years old
    那年我二十岁
    

     以下代码是一些基本操作,可以把相应的注释符去掉,逐一验证

     1 #-*-coding:utf-8-*-
     2 #_author_:Keep
     3 
     4 #data = open('7 years',encoding='utf-8').read()
     5 '''
     6 #只读模式
     7 f = open('7 years','r',encoding='utf-8')#文件句柄
     8 data = f.read()
     9 print(data)
    10 '''
    11 '''
    12 #只写模式
    13 f = open('随笔','w',encoding='utf-8')#w,只写模式,不可读;不存在则创建;存在则删除内容;
    14 f.write('你的孤独,
    ')
    15 f.write('虽败犹荣。')
    16 '''
    17 '''
    18 #追加模式 a = append 追加
    19 f = open('7 years','a',encoding='utf-8')
    20 f.write('
    你的孤独,
    ')
    21 f.write('虽败犹荣。')
    22 f.close()
    23 '''
    24 '''
    25 #读前两行
    26 f = open('7 years','r',encoding='utf-8')
    27 for i in  range(2):
    28     print(f.readline())
    29 '''
    30 '''
    31 f = open('7 years','r',encoding='utf-8')
    32 #print(f.readlines())#把文件转化为列表
    33 for line in  f.readlines():
    34     print(line.strip())#strip去掉换行和空格
    35 '''
    36 '''
    37 #(较low)在第五行后取分割线
    38 f = open('7 years','r',encoding='utf-8')
    39 for index,line in  enumerate(f.readlines()):
    40     if index == 5:
    41         print('---------分割线-----------')
    42         continue
    43     print(line.strip())
    44 '''
    45 #(高逼格)在第五行后取分割线
    46 f = open('7 years','r',encoding='utf-8')
    47 count = 0
    48 for line in f:
    49     if count == 5:
    50         print('------------分割线-----------')
    51     print(line)
    52     count += 1
    View Code

    tell和seek的配合

    f = open('7 years','r',encoding='utf-8')
    print(f.tell())#打印光标所在位置
    print(f.readline())
    print(f.readline())
    print(f.tell())
    f.seek(10)#把光标回到指定位置
    print(f.readline())
    print(f.encoding)
    print(f.name)#打印文件名
    print(f.readable())#判断文件是否可读
    print(f.writable())#判断文件是否可写
    print(f.closed)#判断文件是否关闭

    其他有趣的文件操作

    flush的作用就是每写一行就刷进硬盘,以下是演示过程

     

    #好玩的进度条
    import sys,time#导入sys和time模块
    for i in range(10):
        sys.stdout.write("%")
        sys.stdout.flush()
        time.sleep(1)#时间间隔1s
    截断truncate的用法:
    f = open('7 years','a',encoding='utf-8')
    #f.truncate()#括号什么都不写表示清空文件里的内容
    f.truncate(10)#指定截断
    r+ , a+ , w+, rb, wb的用法:
    #f = open('7 years','r+',encoding='utf-8')#读写
    #f = open('7 years','w+',encoding='utf-8')#写读
    #f = open('7 years','a+',encoding='utf-8')#追加读写
    #f =open("7 years",'rb')#读二进制文件  以下两种情况需要用到此方法:1,网络传输  2,下载
    f = open("7 years","wb")#写二进制文件
    f.write("hello!".encode())
    f.close()
    '''
    print(f.tell())
    f.write("----------7 years-----------")
    '''
    

     文件的修改,以下代码对7 years文件的英文和中文同时修改。

    f = open("7 years",'r',encoding='utf-8')
    f_new = open("7 years.new",'w',encoding='utf-8')
    
    for line in f:
        if "那年我七岁" in line:
            line = line.replace("那年我七岁","那年我八岁")
        if "Once I was seven years old" in line:
            line = line.replace("Once I was seven years old","Once I was eight years old")
        f_new.write(line)
    f.close()
    f_new.close()  

     with的用法:

    #with作用自动关闭文件
    with open("7 years","r",encoding="utf-8") as f:
        for line in f:
            print(line)
    

      



     

  • 相关阅读:
    查找正序排列的List中缺失的日期数据的一个算法
    了解下C#异常时的输出
    Environment.Exit(0) 、Application.Exit() 、this.Close() 、this.Dispose()的区别
    java--多线程之后台线程
    java--线程状态【转】
    DRP项目总结
    Java实现 蓝桥杯 算法训练 矩阵乘法
    Java实现 蓝桥杯 算法训练 矩阵乘法
    Java实现 蓝桥杯 算法训练 矩阵乘法
    Java实现 蓝桥杯 算法训练 大小写转换
  • 原文地址:https://www.cnblogs.com/keepma/p/10428116.html
Copyright © 2011-2022 走看看