文件是用于存储数据的命名位置。例如,main.py
是一个始终用于存储 Python 代码的文件。
Python 提供了各种函数来执行不同的文件操作,这个过程称为文件处理。
在 Python 中打开文件
在 Python 中,我们需要先打开一个文件才能对其执行任何操作——我们使用 open() 函数来做到这一点。让我们看一个例子
假设我们有一个名为 file1.txt
的文件。

要打开此文件,我们可以使用 open()
函数。
file1 = open("file1.txt")
在这里,我们创建了一个名为 file1 的文件对象。现在,我们可以使用此对象来处理文件。
更多关于文件打开
Python 允许我们以不同的模式(读、写、追加等)打开文件,基于这些模式,我们可以执行不同的文件操作。例如,
file1 = open("file1.txt")
在这里,我们以**读取模式**打开文件(我们只能读取内容,不能修改它)。
注意: 默认情况下,Python 文件以读取模式打开。因此,代码 open("file1.txt", "r")
等同于 open("file1.txt")
。
打开 Python 文件的不同模式
模式 | 描述 |
---|---|
r |
以读取模式打开文件(默认) |
w |
以写入模式打开文件 |
x |
为独占创建打开文件 |
a |
以追加模式打开文件(在文件末尾添加内容) |
t |
以文本模式打开文件(默认) |
b |
以二进制模式打开文件 |
+ |
以读写模式打开文件 |
以下是使用不同模式打开文件的几个示例:
# open a file in default mode (read and text)
file1 = open("test.txt") # equivalent to open("test.txt", "rt")
# open a file in write and text mode
file1 = open("test.txt",'w')
# open a file in read, write and binary mode
file1 = open("img.bmp",'+b')
我们也可以使用文件的完整路径来打开它。
file_path = "/home/user/documents/file1.txt"
file1 = open(file_path)
在此示例中,/home/user/documents/file1.txt
是位于 /home/user/documents/
目录中的名为 file1.txt
的文件的完整路径。
注意:要了解有关目录的更多信息,请访问 Python 目录。
在 Python 中读取文件
打开文件后,我们使用 read()
方法来读取其内容。例如,
假设我们有一个名为 file1.txt 的文件。

现在,让我们读取文件的内容。
# open a file in read mode
file1 = open("file1.txt")
# read the file content
read_content = file1.read()
print(read_content)
输出
This is a test file. Hello from the test file.
在上面的示例中,代码 file1.read()
读取文件的内容并将其存储在 read_content 变量中。
注意: 如果您需要关于 file1.read()
代码如何工作的说明,请访问 Python 对象的工作原理。
在 Python 中写入文件
要写入 Python 文件,我们需要使用 w
参数以写入模式打开它。
假设我们有一个名为 file2.txt 的文件。让我们向该文件写入内容。
# open the file2.txt in write mode
file2 = open('file2.txt', 'w')
# write contents to the file2.txt file
file2.write('Programming is Fun.\n')
file2.write('Programiz for beginners\n')
当我们运行上面的代码时,我们将在文件中看到指定的內容。

如果我们尝试对已包含某些内容的文件执行写入操作,新内容将替换现有内容。例如,
假设我们有一个名为 file2.txt 的文件。

现在让我们向该文件写入内容。
# open the file2.txt in write mode
file2 = open('file2.txt', 'w')
# write new contents to the file2.txt file
file2.write('Learning Python is interesting.\n')
file2.write('File operations are useful.\n')
当我们运行上面的代码时,新内容将替换 file2.txt
中的任何现有内容。

注意:如果我们尝试打开一个不存在的文件,将创建一个新文件,并且 write()
方法会将内容添加到文件中。
在 Python 中关闭文件
当我们完成对文件的操作后,我们需要正确关闭文件。我们在 Python 中使用 close()
函数来关闭文件。例如,
# open a file
file1 = open("file1.txt", "r")
# read the file
read_content = file1.read()
print(read_content)
# close the file
file1.close()
输出
This is a test file. Hello from the test file.
注意: 关闭文件将释放与文件关联的资源。因此,始终关闭文件是一种良好的编程习惯。
使用 with...open 打开 Python 文件
在 Python 中,使用 with...open
是打开文件的更好方法。例如,
with open("file1.txt", "r") as file1:
read_content = file1.read()
print(read_content)
输出
This is a test file. Hello from the test file.
在这里,with...open
会自动关闭文件,因此我们无需使用 close()
函数。
注意:由于我们不必担心关闭文件,请养成使用 with...open
语法的习惯。
更多关于 Python 文件操作
如果在处理文件时发生异常,代码将在未关闭文件的情况下退出。因此,使用 try...finally 块是一个好习惯。
让我们看一个例子。
try:
file1 = open("file1.txt", "r")
read_content = file1.read()
print(read_content)
finally:
# close the file
file1.close()
输出
This is a test file. Hello from the test file.
在这里,finally
块将始终执行,因此我们将 close() 函数放在其中。这确保文件将被始终关闭。
要了解有关异常的更多信息,请访问 Python 异常处理。
注意:在处理文件时,请养成使用 with...open
语法的习惯,这样您就不必担心关闭文件了。
文件对象有多种可用方法。其中一些已在上面的示例中使用过。
这是文本模式下方法的完整列表及其简要说明
方法 | 描述 |
---|---|
close() |
关闭已打开的文件 |
read(n) |
读取文件内容 |
seek(offset,from=SEEK_SET) |
将文件位置更改为 offset 字节,相对于 from (开始、当前、结束) |
tell() |
返回一个整数,表示文件对象的当前位置 |
write(s) |
将字符串写入文件 |
writelines(lines) |
将 lines 列表写入文件 |