Python file
File Operation Flow
1. Open the file, get the file handle and assign a value to a variable
2. Manipulate the file through a handle
3. Close the file
Mode of open File
R, read-only mode "default"
W, write-only mode "unreadable; not exist" created; empty content;
X, write-only mode "unreadable; not present, create, present error"
A, append mode "readable; not present, create; append content only;"
"+" means you can read and write a file at the same time
r+, read and write "readable, writable"
w+, write "readable, writable"
x+, write "readable, writable"
A +, write "readable, writable"
"B" means to operate in bytes
RB or R+b
WB or W+b
XB or W+b
AB or A+b
Note: When opened in B, the content read is byte type, and the byte type is also required for writing
With open ("test.txt") as F can not be active to close the file, the program will automatically close the file
The point is to read and write this piece of attention
The contents of the existing file are as follows:
123456789 |
Somehow, it seems the love I knew was always the most destructive kind Yesterday when I was young The taste of life was sweet As rain upon my tongue I teased at life as if it were a foolish game The way the evening breeze May tease the candle flame The thousand dreams I dreamed The splendid things I planned |
Read the contents of the file by reading:
12345678910111213 |
#read将文件内容一次性全部读取出来
>>> with
open
(
"test.txt"
) as f:
...
print (f.read())
...
Somehow, it seems the love I knew was always the most destructive kind
Yesterday when I was young
The taste of life was sweet
As rain upon my tongue
I teased at life as
if it were a foolish game
The way the evening breeze
May tease the candle flame
The thousand dreams I dreamed
The splendid things I planned
|
To read the contents of a file by means of ReadLine:
123456789101112131415161718192021222324252627 |
#readline是将文件内容一行行的读取,每执行一次读取一行
>>> with
open
(
"test.txt"
) as f:
... flag
= True
...
while flag:
... line
= f.readline()
...
if line:
...
print (line)
...
else
:
... flag
= False
...
Somehow, it seems the love I knew was always the most destructive kind
Yesterday when I was young
The taste of life was sweet
As rain upon my tongue
I teased at life as
if it were a foolish game
The way the evening breeze
May tease the candle flame
The thousand dreams I dreamed
The splendid things I planned
|
To read the contents of a file by means of ReadLines:
123456789101112131415161718192021222324252627 |
#readlines是将整个文件内容以列表的形式读取出来
>>> with
open
(
"test.txt"
) as f:
...
print (f.readlines())
...
[
‘Somehow, it seems the love I knew was always the most destructive kind\n‘
,
‘Yesterday when I was young\n‘
,
‘The taste of life was sweet\n‘
,
‘As rain upon my tongue\n‘
,
‘I teased at life as if it were a foolish game\n‘
,
‘The way the evening breeze\n‘
,
‘May tease the candle flame\n‘
,
‘The thousand dreams I dreamed\n‘
,
‘The splendid things I planned\n‘
]
#遍历文件内容
>>> with
open
(
"test.txt"
) as f:
...
for line
in f.readlines():
...
print (line)
...
Somehow, it seems the love I knew was always the most destructive kind
Yesterday when I was young
The taste of life was sweet
As rain upon my tongue I teased at life as
if it were a foolish game
The way the evening breeze
May tease the candle flame
The thousand dreams I dreamed
The splendid things I planned
|
Traverse file Contents directly:
123456789101112131415161718192021 |
>>> with
open
(
"test.txt"
) as f:
...
for line
in f:
...
print (line)
...
Somehow, it seems the love I knew was always the most destructive kind
Yesterday when I was young
The taste of life was sweet
As rain upon my tongue I teased at life as
if it were a foolish game
The way the evening breeze
May tease the candle flame
The thousand dreams I dreamed
The splendid things I planned
|
Here is the file content after opening the file in various ways after writing "Hello World"
The contents of the file after the write is opened in a r+ way (you can see that the characters in front of the file are replaced):
123456789 |
hello world seems the love I knew was always the most destructive kind Yesterday when I was young The taste of life was sweet As rain upon my tongue I teased at life as if it were a foolish game The way the evening breeze May tease the candle flame The thousand dreams I dreamed The splendid things I planned |
Open the contents of the file after the write operation in a w+ way (you can see that the contents are emptied and then written):
Open the file for read-write operation in a A + (you can see Hello World appended to the last side of the file):
123456789 |
Somehow, it seems the love I knew was always the most destructive kind Yesterday when I was young The taste of life was sweet As rain upon my tongue I teased at life as if it were a foolish game The way the evening breeze May tease the candle flame The thousand dreams I dreamed The splendid things I plannedhello world |
Python file operations