在 Python 中,有几种方法可以读取文本文件。

在本文中,我将介绍 open() 函数、read() 方法、readline() 方法、readlines() 方法、close() 方法和 with 关键字。

Python 中的 open() 函数是什么

如果要在 Python 中读取文本文件,首先必须打开它。

这是 Python 的 open() 函数的基本语法:

open("name of file you want opened", "optional mode")

文件名和正确路径

如果文本文件和你当前的文件在同一目录(“文件夹”)中,那么你只需在 open() 函数中引用文件名即可。

open("demo.txt")

以下是两个文件位于同一目录中的示例:

Screen-Shot-2021-09-13-at-1.49.16-AM

如果你的文本文件位于不同的目录中,则你需要引用文本文件的正确路径名。

在此示例中,random-text 文件位于与 main.py 不同的文件夹中:

Screen-Shot-2021-09-13-at-2.00.27-AM

为了在 main.py 中访问该文件,你必须在文件名中包含文件夹名称。

open("text-files/random-text.txt")

如果你没有正确的文件路径,那么你将收到如下错误消息:

open("random-text.txt")
Screen-Shot-2021-09-13-at-2.03.33-AM

跟踪你所在的目录非常重要,这样你可以引用正确的路径名。

open() 中的可选模式参数

处理文件时有不同的模式。默认模式是读取模式。

字母 r 代表读取模式。

open("demo.txt", mode="r")

你也可以省略 mode=,只用 "r"

open("demo.txt", "r")

还有其他类型的模式,例如用于写入的 "w" 或用于追加的 "a"。我不会详细介绍其他模式,因为我们只会专注于读取文件。

有关其他模式的完整列表,请阅读文档

Python 中 open() 函数的其他参数

open() 函数可以接收其他可选参数:

  • buffering
  • encoding
  • errors
  • newline
  • closefd
  • opener

阅读文档了解更多参数。

Python 中的 readable() 方法

如果要检查文件是否可以读取,则可以使用 readable() 方法。这将返回 TrueFalse

此示例将返回 True,因为我们处于读取模式:

file = open("demo.txt")
print(file.readable())
Screen-Shot-2021-09-13-at-3.36.37-AM

如果我将此示例更改为 "w"(写入)模式,则 readable() 方法将返回 False

file = open("demo.txt", "w")
print(file.readable())
Screen-Shot-2021-09-13-at-3.36.18-AM

Python 中的 read() 方法

read() 方法会将文件的所有内容作为一个字符串读取。如果文本文件中的内容不多,这是一个很好的方法。

在本例中,我使用 read() 方法从 demo.txt 文件中打印出名称列表:

file = open("demo.txt")
print(file.read())
Screen-Shot-2021-09-13-at-2.43.59-AM

此方法可以接收一个名为 size 的可选参数。不是读取整个文件,而是只读取其中的一部分。

如果我们修改前面的例子,可以通过添加数字 4 作为 read() 的参数,只打印出第一个单词。

file = open("demo.txt")
print(file.read(4))
Screen-Shot-2021-09-13-at-3.01.30-AM

如果省略 size 参数,或者数字为负数,则将读取整个文件。

Python 中的 close() 方法

完成读取文件后,关闭它很重要。如果你忘记关闭文件,则可能会导致问题。

这是如何关闭 demo.txt 文件的示例:

file = open("demo.txt")
print(file.read())
file.close()

如何使用 with 关键字在 Python 中关闭文件

确保文件关闭的一种方法是使用 with 关键字。这被认为是一种很好的做法,因为文件会自动关闭,而你不必手动关闭它。

以下是如何使用 with 关键字重写我们的示例:

with open("demo.txt") as file:
    print(file.read())

Python 中的 readline() 方法

此方法将从文件中读取一行并返回。

在这个例子中,我们有一个包含这两个句子的文本文件:

This is the first line
This is the second line

如果我们使用 readline() 方法,它只会打印文件的第一句话。

with open("demo.txt") as file:
    print(file.readline())
Screen-Shot-2021-09-13-at-3.57.14-AM

此方法还接受可选的 size 参数。我们可以修改例子,加上数字 7 来只读取和打印出来This is

with open("demo.txt") as file:
    print(file.readline(7))
Screen-Shot-2021-09-13-at-4.08.03-AM

Python 中的 readlines() 方法

此方法将读取并返回文件中所有行的列表。

在此示例中,我们将使用 readlines() 方法将杂货项目打印为列表。

with open("demo.txt") as file:
    print(file.readlines())
Screen-Shot-2021-09-13-at-4.19.23-AM

如何使用 for 循环从 Python 中的文件中读取行

这些不同读取方法的替代方法是使用 for 循环。

在这个例子中,我们可以通过循环对象打印出 demo.txt 文件中的所有项目。

with open("demo.txt") as file:
    for item in file:
        print(item)
Screen-Shot-2021-09-13-at-4.27.49-AM

总结

如果要在 Python 中读取文本文件,首先必须打开它。

open("name of file you want opened", "optional mode")

如果文本文件和你当前的文件在同一目录(“文件夹”)中,那么你只需在 open() 函数中引用文件名即可。

如果你的文本文件位于不同的目录中,则你需要引用文本文件的正确路径名。

open() 函数接受可选的 mode(模式)参数。默认模式是读取模式。

open("demo.txt", "r")

如果要检查文件是否可以读取,则可以使用 readable() 方法。这将返回 TrueFalse

file.readable()

read() 方法会将文件的所有内容作为一个字符串读取。

file.read()

完成读取文件后,关闭它很重要。如果你忘记关闭文件,则可能会导致问题。

file.close()

确保文件关闭的一种方法是使用 with 关键字。

with open("demo.txt") as file:
    print(file.read())

readline() 方法将从文件中读取一行并返回。

file.readline()

readlines() 方法将读取并返回文件中所有行的列表。

file.readlines()

这些不同读取方法的替代方法是使用 for 循环。

with open("demo.txt") as file:
    for item in file:
        print(item)

我希望你喜欢这篇文章,并祝你享受 Python 之旅!

原文:Python Open File – How to Read a Text File Line by Line,作者:Jessica Wilkins