from sys import stdout ,stdin

f=open(r"c:\text\somefile.txt")

open(filename,mode,buffering)

  mode

    'r'  read

    'w'  writ

    'a'  追加模式

    'b'  二进制模式

    '+'  读写模式

  buffering

    0 / False   无缓冲,直接读写 硬盘

    1 / True     缓冲,用内存代替硬盘,只有在close/flush才更新硬盘数据

    -1    表示使用默认缓冲区

    大于1   表示缓冲区大小

  基本文件方法

    文件和类文件(支持部分文件方法),有时候也称为流。

    sys.stdin             标准文件输入流

    sys.stdout   标准文件输出流

    sys.stderr   标准错误流

  读和写

    f=open("somefile.txt",'w')

    f.write('hello,word')

    f.close()

    

    f=open("somefile.txt","r")

    f.read()

    file.readline()  读取一行     file.readline(n)   n为非负整数,表示读取的字符(字节)最大值

    file.readlines()读取所有行,并作为列表返回

  关闭文件

    file.close()

    with open() as file:

      close(file)

 

相关文章:

  • 2022-01-15
  • 2021-06-09
  • 2021-12-23
猜你喜欢
  • 2022-01-15
  • 2021-11-27
  • 2021-11-08
  • 2021-04-23
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
相关资源
相似解决方案