Python File write() 方法将字符串写入文件。当该方法将字符串写入此文件时,它首先被写入内部缓冲区;一旦此缓冲区已满,内容就会传输到当前文件。由于这种缓冲,在调用 flush() 或 close() 方法之前,字符串实际上可能不会显示在文件中。
如果 current 为空,则该方法从头开始向其添加内容;但是,如果它已经包含一些文本,则此文本添加到文件中的位置取决于文件指针的位置;此位置因文件模式而异。各种场景如下 -
- 如果以写入模式(w 或 w+)打开文件,则会从文件中擦除现有文本,并且文件指针将移动到文件的开头。因此,在这种情况下, write() 方法从头开始写入。
- 如果文件以追加模式(a 或 a+)打开,则现有文本将保持原样,并且文件指针将保留在文件末尾。因此,新文本将添加到现有内容之后。
- 如果文件以读写模式 (r+) 打开,则指针将移动到文件的开头并替换文件中的现有内容。但是,整个文本不会被替换;相反,write() 方法仅将现有字符串中的字符替换为新字符串的相应索引中的字符。
- 如果文件以读取模式 (r) 打开,则此方法无效。
语法
以下是 Python File write() 方法的语法 -
fileObject.write(str)
参数
- str − 这是要写入文件中的 String。
返回值
此方法不返回任何值。
例考虑一个包含字符串的演示文件 “foo.txt”。
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
以下示例显示了 Python File write() 方法的用法。演示文件以写入 (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
str = "This is the new line"
line = fo.write( str )
print("Check the file to see the reflected changes")
# Close opened file
fo.close()
当我们运行上述程序时,它会产生以下结果——
Check the file to see the reflected changes
例
在上面的示例中,必须打开文件以检查更改是否已反映。但是我们也可以尝试在终端本身中显示文件内容。为此,一旦使用 write() 方法重写文件,我们就会关闭该文件。然后,此文件在 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
fo.write("This is the new line")
# 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()
执行上述程序时,输出显示在终端上,如下所示 -
File Contents: This is the new line
例
如果我们尝试使用 write (w) 模式打开目录中不存在的文件,则会创建一个具有给定名称的新文件。在以下示例中,对此文件调用 write() 方法,并将新内容插入其中。
# Open a non-existent file in write mode
fo = open("new.txt", "w")
print("Name of the file: ", fo.name)
fo.write("This is the new line")
print("Check the file to see the reflected changes")
# Close opened file
fo.close()
让我们编译并运行给定的程序,输出显示如下 -
Check the file to see the reflected changes
例
write() 方法也可以在 append (a 或 a+) 模式下打开文件时使用。
在此示例中,名为 “foo.txt” 的文件以附加模式 “a” 打开。通常,如果我们想将内容附加到文件中,我们会使用 seek() 方法将文件指针移动到末尾,但这里没有必要。由于文件是以 append 模式打开的,因此 write() 方法在文件末尾写入内容。文件内容显示在终端本身中。
# 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)
line = fo.write("This is the new line")
# 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()
程序编译并运行后,输出将显示在终端中,如下所示 -
File Contents: Hello!
This is qikepu
WelcomeThis is the new line
例
如果我们对使用读写模式 (r+) 打开的文件使用 write() 方法,则指针会移动到文件的开头并替换文件中的现有内容。
在下面的示例中,我们在 “r+” 模式下打开一个现有文件 “foo.txt”。write() 方法将文件开头的现有字符串的字符替换为传递给它的新字符串参数的相应索引。
# Open the test file in append mode
fo = open("foo.txt", "r+")
print("Name of the file: ", fo.name)
fo.write("This is a new line")
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()
让我们编译并运行上面的程序,以产生如下结果 -
File Contents: This is a new lineThis is 2nd line
This is 3rd line
This is 4th line
This is 5th line