zoukankan      html  css  js  c++  java
  • 文件管理

    一、文件管理的两种方法  

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

      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 :要写入的存储模型信息,模型信息可以为字符串
      
      
      
      

  • 相关阅读:
    golang mod 导包
    grpc client 报错: code = Unimplemented desc = method *** not implemented
    golang读取email
    docker 使用
    在word中批量更改Mathtype公式的格式
    word中插入myth type公式行距变大的问题
    word中编辑论文公式对齐问题
    向别人学习
    机器学习 博文汇总
    matlab中如何用rand产生相同的随机数
  • 原文地址:https://www.cnblogs.com/wangxiongbing/p/9901800.html
Copyright © 2011-2022 走看看