Python3 File write() Method

Python3 File(文件) 方法Python3 File Methods


Overview

write()The method is used to write 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.

If the file open mode includes b, then when writing file content, str (parameter) must be converted to bytes form using the encode method, otherwise an error is raised: TypeError: a bytes-like object is required, not 'str'.

Syntax

The syntax of the write() method is as follows:

fileObject.write( [ str ])

Parameters

  • fileObject-- file object, usually obtained by opening the 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 pointer position of the file:

  • If the file is opened in append mode ("a"or"a+"), the written content will be added to the end of the file.

  • If the file is opened in read-write mode ("r+"or"w+"), the written content will overwrite the original content starting from the current file pointer position.

Return Value

Returns the number of characters written.

Example

The content of the file example.txt is as follows:

1:www.example.com
2:www.example.com
3:www.example.com
4:www.example.com
5:www.example.com

The following example demonstrates the use of the write() method:

Example

#!/usr/bin/python3

# Use the with statement to open the file, ensuring it is closed properly
with open("example.txt", "r+") as fo:
    print("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.example.com\n")  # Write content and add a newline

    # Read all content of the file
    fo.seek(0)  # Move the file pointer to the beginning of the file
    for index, line in enumerate(fo):
        print("File line %d - %s" % (index, line.strip()))

The output of the above example is:

文件行号 0 - 1:www.example.com
文件行号 1 - 2:www.example.com
文件行号 2 - 3:www.example.com
文件行号 3 - 4:www.example.com
文件行号 4 - 5:www.example.com
文件行号 5 - 6:www.example.com

View the file content:

$ cat example.txt 
1:www.example.com
2:www.example.com
3:www.example.com
4:www.example.com
5:www.example.com
6:www.example.com

Notes

  1. File Modes:

    • If the file is opened in read-only mode ("r"), callingwrite()will raiseio.UnsupportedOperationan exception.

    • If the file is opened in write mode ("w"or"w+"), the file content will be cleared, and then new content will be written.

    • If the file is opened in append mode ("a"or"a+"), the written content will be added to the end of the file.

  2. File Pointer:

    • The write operation starts from the current file pointer position. If you need to write from the beginning or a specific position, you can use theseek()method to move the file pointer.

  3. Newline Character:

    • write()The method does not automatically add a newline. If a newline is needed, you must explicitly add\n。

  4. Return Value:

    • write()The method returns the number of characters written. If the write fails, an exception may be raised.


Python3 File(文件) 方法Python3 File Methods

Other Extensions