Python File IO
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
(
)
(
text
)
The output of the code above is:
该文本会写入到文件中
看到我了吧!
Python3 Examples