Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 File write() Method

Python3 File write() Method

Python3 File Methods Python3 File methods


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

The write() method writes the specified string to the file; the write position depends on 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:

   

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.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

)

:

       

print

(

"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

  1. File mode:

  2. If the file is opened in read-only mode ("r"), calling write() will throw io.UnsupportedOperation exception.

  3. If the file is opened in write mode ("w" or "w+"), the file contents are cleared and new contents are written.

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

  5. -

    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.

    1. File pointer:

    2. 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.

    3. -

      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.

      1. Newline:

      2. write() The method does not add a newline automatically. If a newline is needed, it must be added explicitly in the string \n。

      3. -

        write() The method does not add a newline automatically. If a newline is needed, it must be added explicitly in the string \n。

        1. Return Value:

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

        3. -

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


          Python3 File Methods Python3 File methods