Python File writelines() 方法


Python File writelines() 方法将一系列字符串写入该文件。序列可以是生成字符串的任何可迭代对象,通常是字符串列表。

当该方法将字符串序列写入此文件时,它首先被写入内部缓冲区;一旦此缓冲区已满,内容就会传输到当前文件。由于这种缓冲,在调用 flush() 或 close() 方法之前,文本实际上可能不会显示在文件中。

如果 current 为空,则该方法从头开始向其添加内容;但是,如果它已经包含一些文本,则此文本添加到文件中的位置取决于文件指针的位置;此位置因文件模式而异。各种场景如下 -

  • 如果以写入模式(w 或 w+)打开文件,则会从文件中擦除现有文本,并且文件指针将移动到文件的开头。因此,在这种情况下, writelines() 方法从头开始写入。
  • 如果文件以追加模式(a 或 a+)打开,则现有文本将保持原样,并且文件指针将保留在文件末尾。因此,新文本将添加到现有内容之后。
  • 如果文件以读写模式 (r+) 打开,则指针将移动到文件的开头并替换文件中的现有内容。但是,整个文本不会被替换;相反,writelines() 方法仅将现有字符串中的字符替换为新字符串对象的字符。文件中的其余文本保持不变。
  • 如果文件以读取模式 (r) 打开,则此方法无效。

语法

以下是 Python File writelines() 方法的语法 -


 fileObject.writelines( sequence )

参数

  • sequence − 这是字符串的 Sequence。

返回值

此方法不返回任何值。

例

考虑一个包含字符串的演示文件 “foo.txt”。

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line

以下示例显示了 Python File writelines() 方法的用法。演示文件以写入 (w) 模式打开,因此文件中的现有内容将被擦除,并使用此方法插入新内容。由于此方法不返回任何值,因此必须直接检查文件以查看反映的更改。


# Open a file in write mode
fo = open("foo.txt", "w")
print("Name of the file: ", fo.name)

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

# Write a new line into the file
seq = ["This is the new line", '\n', "This is another new line"]
line = fo.writelines(seq)

print("Check the file to see the reflected changes")

# Close opened file
fo.close()

当我们运行上述程序时,它会产生以下结果——

Name of the file: foo.txt
Check the file to see the reflected changes

例

在上面的示例中,必须打开文件以检查更改是否已反映。但是我们也可以尝试在终端本身中显示文件内容。为此,一旦使用 writelines() 方法重写了文件,我们就会关闭该文件。然后,此文件在 read (r) 模式下再次打开,并使用 read() 方法读取文件内容。


# Open a file in write mode
fo = open("foo.txt", "w")
print("Name of the file: ", fo.name)

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

# Write a new line
seq = ["This is the new line", '\n', "This is another new line"]
line = fo.writelines(seq)

# Close opened file
fo.close()

# Open the file again in read mode
fo = open("foo.txt", "r")

line = fo.read()
print("File Contents:", line)

# Close opened file again
fo.close()

执行上述程序时,输出显示在终端上,如下所示 -

Name of the file: foo.txt
File Contents: This is the new line
This is another new line

例

如果我们尝试使用 write (w) 模式打开目录中不存在的文件,则会创建一个具有给定名称的新文件。在下面的示例中,对此文件调用 writelines() 方法,并将新内容插入其中。


# Open a non-existent file in write mode
fo = open("new.txt", "w")
print("Name of the file: ", fo.name)

# Write a new text
seq = ["Hello", '\n', "Welcome to qikepu"]
line = fo.writelines(seq)

print("Check the file to see the reflected changes")

# Close opened file
fo.close()

让我们编译并运行给定的程序,输出显示如下 -

Name of the file: new.txt
Check the file to see the reflected changes

例

writelines() 方法也可以在 append (a 或 a+) 模式下打开文件时使用。

在此示例中,名为 “foo.txt” 的文件以附加模式 “a” 打开。由于文件是在 append 模式下打开的,因此 writelines() 方法在文件末尾写入内容。使用 write() 方法,新行将紧跟在字符串的最后一个字符之后;但是使用 writelines() 方法,当换行符和字符串作为对象传递给列表时,新字符串将附加到下一行。文件内容显示在终端本身中。


# Let the test file contain the following strings
# Hello!
# This is qikepu
# Welcome

# Open the test file in append mode
fo = open("test.txt", "a")
print("Name of the file: ", fo.name)

seq = ['\n', "This is the new line"]
fo.writelines(seq)

# Close opened file
fo.close()

# Open the file again in read mode
fo = open("test.txt", "r")

line = fo.read()
print("File Contents:", line)

# Close opened file again
fo.close()

程序编译并运行后,输出将显示在终端中,如下所示 -

Name of the file: test.txt
File Contents: Hello!
This is qikepu
Welcome
This is the new line

例

如果我们对使用读写模式 (r+) 打开的文件使用 writelines() 方法,则指针会移动到文件的开头并替换文件中的现有内容。

在下面的示例中,我们在 “r+” 模式下打开一个现有文件 “foo.txt”。文件中的现有字符将从开头替换为作为列表传递的字符串对象中的字符。文件中的其余文本保持不变。


# Open the test file in append mode
fo = open("foo.txt", "r+")
print("Name of the file: ", fo.name)

seq = ["This is a new line", '\n', "This is another new line", '\n']
fo.writelines(seq)

print("Check the file to see the reflected changes")

# Close opened file again
fo.close()

# Open the file again in read mode
fo = open("foo.txt", "r")

line = fo.read()
print("File Contents:", line)

# Close opened file again
fo.close()

让我们编译并运行上面的程序,以产生如下结果 -

Name of the file: foo.txt
Check the file to see the reflected changes
File Contents: This is a new line
This is another new line
d line
This is 4th line
This is 5th line