• 文件管理


    一、文件管理的两种方法  

      方法一:打开文件和关闭文件都要有,一般书写时,先写文件的打开和关闭,在写关于内容的操作

      filename = open("1.txt","r",endcodeing= "utf-8")

      filename.read()

      filename.close()

      方法二、用 with    as 打开,,该方法自动关闭文件

      with open("1.txt","w",endcodeing= "utf-8")  as file:

        file.write("人生苦短,我要学python!")

    二、文件的读写方法如下:

    三、将写入的文件读出来对比,看写的是否成功(比较漏)

      file = open("test2.txt", "w")
      info = file.write("人生苦短,我用python")
      file.close()

      file = open("test2.txt", "r")
      list1 = file.read(info)
      print(list1)
      file.close()

    四、复制文件操作

      filename1 = "d:/text4.txt"
      index1 = filename1.rfind(".")
      filenaem2 = filename1[0:index1] +" - 副本 "+ filename1[index1:]
      file1 = open(filename1, "rb")
      file2 = open(filenaem2, "wb")

      while 1:
      info = file1.read(1024)
      if len(info) == 0:
      break
      else:
      file2.write(info)

      file1.close()
      file2.close()


    五、操作文件常用的方法
    1、读文件操作
      read()
      read(num)
      readline() 按行读,以换行符作为结束标志
      readlines() 按行读取文件,将读到的文件存入列表中,以换行符作为结束标志

    2、写文件操作
      write()
      writelines(model) model :要写入的存储模型信息,模型信息可以为字符串
      
      
      
      

  • 相关阅读:
    bzoj 3155: Preprefix sum
    bzoj 1854: [Scoi2010]游戏
    UVA1608 不无聊的序列 Non-boring sequences
    UVA1747 【Swap Space】
    Luogu P5550 Chino的数列
    bzoj 1799: [Ahoi2009]self 同类分布
    bzoj 1054: [HAOI2008]移动玩具
    MATLAB工具箱,应用程序,软件和资源的精选清单
    论文格式排版Issue及解决办法
    《将博客搬至CSDN》
  • 原文地址:https://www.cnblogs.com/wangxiongbing/p/9901800.html
Copyright © 2020-2023  润新知