Python3 File write() Method
Overview
write() The method writes a specified string to a file.
Before the file is closed or the buffer is flushed, the string content is stored in the buffer, so you cannot see the written content in the file yet.
If the file is opened in a mode with b, the str argument must be converted to bytes with encode before writing, otherwise an error occurs: TypeError: a bytes-like object is required, not 'str'.
Syntax
The write() method syntax is as follows:
fileObject.write( [ str ])
Parameters
-
fileObject -- a file object, usually obtained by opening a file with the open() function.
-
str -- the string to write to the file.
The write() method writes the specified string to the file; the write position depends on the current file pointer position:
-
If the file is opened in append mode (
"a"or"a+"), the written content is added to the end of the file. -
If the file is opened in read/write mode (
"r+"or"w+"), the written content overwrites the existing content starting at the current file pointer position.
Return Value
Returns the number of characters written.
Example
The contents of the runoob.txt file are as follows:
1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com
The following example shows how to use the write() method:
Example
#!/usr/bin/python3
Open the file with a with statement to ensure it is closed properly
with
open
(
"runoob.txt"
,
"r+"
)
as
fo:
(
"File name: "
,
fo.
name
)
Write a line at the end of the file
fo.
seek
(
0
,
2
)
Move the file pointer to the end of the file
fo.
write
(
"6:www.runoob.com\n"
)
Write content and add a newline character
Read all the file contents
fo.
seek
(
0
)
Move the file pointer to the beginning of the file
for
index
,
line
in
enumerate
(
fo
)
:
(
"File line %d - %s"
%
(
index
,
line.
strip
(
)
)
)
The output of the above example is:
文件行号 0 - 1:www.runoob.com
文件行号 1 - 2:www.runoob.com
文件行号 2 - 3:www.runoob.com
文件行号 3 - 4:www.runoob.com
文件行号 4 - 5:www.runoob.com
文件行号 5 - 6:www.runoob.com
View the file contents:
$ cat runoob.txt
1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com
6:www.runoob.com
Notes
-
File mode:
If the file is opened in read-only mode ("r"), calling write() will throw io.UnsupportedOperation exception.
If the file is opened in write mode ("w" or "w+"), the file contents are cleared and new contents are written.
If the file is opened in append mode ("a" or "a+"), the written content is added to the end of the file.
If the file is opened in read-only mode ("r"), calling write() will throw io.UnsupportedOperation exception.
If the file is opened in write mode ("w" or "w+"), the file contents are cleared and new contents are written.
If the file is opened in append mode ("a" or "a+"), the written content is added to the end of the file.
-
File pointer:
Writing starts at the current file pointer position. To write from the beginning of the file or a specific position, use the seek() method to move the file pointer.
Writing starts at the current file pointer position. To write from the beginning of the file or a specific position, use the seek() method to move the file pointer.
-
Newline:
write() The method does not add a newline automatically. If a newline is needed, it must be added explicitly in the string \n。
write() The method does not add a newline automatically. If a newline is needed, it must be added explicitly in the string \n。
-
Return Value:
write() The method returns the number of characters written. If writing fails, an exception may be raised.
write() The method returns the number of characters written. If writing fails, an exception may be raised.
Python3 File methods