[open()](https://docs.python.org/zh-cn/3/library/functions.html#open)

Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError

[open()](https://docs.python.org/zh-cn/3/library/functions.html#open) 最常用的有两个参数: open(filename, mode)

  • filename:第一个参数是包含文件名的字符串
  • mode:

image.png

  • 注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。

    open()案例

    1. # f为文件对象
    2. # workfile:包含文件名称和路径的文件全称,可以是绝对或者相对路径
    3. # w:文件的读取模式
    4. f = open('workfile', 'w')
    5. .... #文件对象的操作
    6. f.close() #文件对象操作完成后,必须关闭文件对象

    [with](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#with) open()

  • 在处理文件对象时,最好使用 with 关键字。 优点是当子句体结束后文件会自动正确关闭,即使在某个时刻引发了异常。 而且使用 with 相比等效的 try-finally 代码块要简短得多

[with](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#with) open()案例

  1. # f是文件对象
  2. with open('workfile') as f:
  3. # 通过文件对象方法读取数据
  4. read_data = f.read()

open()完整的语法格式为:

open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True,opener=None)
参数说明:

  • file: 必需,文件路径(相对或者绝对路径)。
  • mode: 可选,文件打开模式
  • buffering: 设置缓冲
  • encoding: 一般使用utf8
  • errors: 报错级别
  • newline: 区分换行符
  • closefd: 传入的file参数类型
  • opener:

    文件对象方法

    file.close()

    | file.close()
    关闭文件。关闭后文件不能再进行读写操作。 | | —- |

文件读方法

file.read([size])

file.read([size])
从文件读取指定的字节数,如果未给定或为负则读取所有。

file.readline([size])

file.readline([size])
读取整行,包括 “\n” 字符。

file.readline()案例

  1. # 文件 runoob.txt 的内容如下:
  2. # 1:www.runoob.com
  3. # 2:www.runoob.com
  4. # 3:www.runoob.com
  5. # 4:www.runoob.com
  6. # 5:www.runoob.com
  7. # 读取文件的内容:
  8. # 实例:file_readline.py
  9. #!/usr/bin/python3
  10. # 打开文件
  11. fo = open("runoob.txt", "r+") #r+ 表示从头开始以读写的方式打开文件
  12. print ("文件名为: ", fo.name)
  13. line = fo.readline() #读取第一行
  14. print ("读取第一行 %s" % (line))
  15. line = fo.readline(5) # 读取第一行的前五个字符
  16. print ("读取的字符串为: %s" % (line))
  17. # 关闭文件
  18. fo.close()
  19. 以上实例输出结果为:
  20. 文件名为: runoob.txt
  21. 读取第一行 1:www.runoob.com
  22. 读取的字符串为: 2:www

file.readlines([sizeint])

file.readlines([sizeint])
读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比 sizeint 较大, 因为需要填充缓冲区。

file.readlines()案例

  1. # 文件 runoob.txt 的内容如下:
  2. # 1:www.runoob.com
  3. # 2:www.runoob.com
  4. # 3:www.runoob.com
  5. # 4:www.runoob.com
  6. # 5:www.runoob.com
  7. # 循环读取文件的内容:
  8. #!/usr/bin/python3
  9. # 打开文件
  10. fo = open("runoob.txt", "r")
  11. print ("文件名为: ", fo.name)
  12. for line in fo.readlines(): #依次读取每行
  13. line = line.strip() #去掉每行头尾空白
  14. print ("读取的数据为: %s" % (line))
  15. # 关闭文件
  16. fo.close()
  17. # 以上实例输出结果为:
  18. 文件名为: runoob.txt
  19. 读取的数据为: 1:www.runoob.com
  20. 读取的数据为: 2:www.runoob.com
  21. 读取的数据为: 3:www.runoob.com
  22. 读取的数据为: 4:www.runoob.com
  23. 读取的数据为: 5:www.runoob.com

文件写方法

file.write(str)

file.write(str)
参数
str — 要写入文件的字符串。返回值
返回的是写入的字符长度。

file.write(str)案例

  1. # 文件runoob.txt的内容如下:
  2. #
  3. # 1: www.runoob.com
  4. # 2: www.runoob.com
  5. # 3: www.runoob.com
  6. # 4: www.runoob.com
  7. # 5: www.runoob.com
  8. #
  9. # 以下实例演示了write()方法的使用:
  10. #!/usr/bin/python3
  11. # 打开文件
  12. fo = open("runoob.txt", "r+")
  13. str = "6:www.runoob.com"
  14. # 在文件末尾写入一行
  15. fo.seek(0, 2)
  16. line = fo.write( str )
  17. # 查看文件内容:
  18. $ cat runoob.txt
  19. 1:www.runoob.com
  20. 2:www.runoob.com
  21. 3:www.runoob.com
  22. 4:www.runoob.com
  23. 5:www.runoob.com
  24. 6:www.runoob.com # 新写入的一行文本

file.writelines(sequence)

| file.writelines(sequence)
向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。
- 参数
str — 要写入文件的字符串序列。
- 返回值
该方法没有返回值 | | —- | | |

file.writelines()案例

  1. #!/usr/bin/python3
  2. # 打开文件
  3. fo = open("test.txt", "w")
  4. print ("文件名为: ", fo.name)
  5. seq = ["菜鸟教程 1\n", "菜鸟教程 2"]
  6. fo.writelines( seq )
  7. # 关闭文件
  8. fo.close()
  9. # 查看文件内容:
  10. $ cat test.txt
  11. 菜鸟教程 1
  12. 菜鸟教程 2