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

Python File IO

Python File IO

Document Object Reference Manual Python3 Examples

The following code demonstrates basic Python file operations, including open, read, and write:

Python with keyword reference:https://www.runoob.com/python3/python3-with-keyword.html

Example (Python 3.0+)

Filename : test.py

author by : www.runoob.com

Write to a file

with

open

(

"

test.txt

"

,

"

wt

"

)

as

out_file

:

out_file

.

write

(

"

This text will be written to the file

\n

You see me!

"

)

Read a file

with

open

(

"

test.txt

"

,

"

rt

"

)

as

in_file

:

text

=

in_file

.

read

(

)

print

(

text

)

The output of the code above is:


该文本会写入到文件中
看到我了吧!

Document Object Reference Manual Python3 Examples