码迷,mamicode.com
首页 > 编程语言 > 详细

Python文件操作

时间:2016-09-05 00:05:45      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

Python文件

 

文件操作流程

    1、打开文件,得到文件句柄并赋值给一个变量

    2、通过句柄对文件进行操作

    3、关闭文件

打开文件的模式

  • r ,只读模式【默认】

  • w,只写模式【不可读;不存在则创建;存在则清空内容;】

  • x, 只写模式【不可读;不存在则创建,存在则报错】

  • a, 追加模式【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+, 读写【可读,可写】

  • w+,写读【可读,可写】

  • x+ ,写读【可读,可写】

  • a+, 写读【可读,可写】

 "b"表示以字节的方式操作

  • rb  或 r+b

  • wb 或 w+b

  • xb 或 w+b

  • ab 或 a+b

 注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型

通过with open("test.txt") as f 可以不用主动去关闭文件,程序会自动对文件进行关闭

重点是读写这块需要注意下

现有文件内容如下:

1
2
3
4
5
6
7
8
9
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的方式去读取文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
#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

通过readline的方式去读取文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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

通过readlines的方式去读取文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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

直接遍历文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> 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

下面是以各种方式打开文件之后写入"hello world"之后的文件内容

以 r+ 的方式打开文件进行写操作后的内容(可以看到文件的前面的字符被替换了):

1
2
3
4
5
6
7
8
9
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

以w+的方式打开文件进行写操作后的内容(可以看到里面的内容被清空后再写入的):

1
hello world

以a+的方式打开文件进行读写操作后的内容(可以看到hello world追加到了文件最后面):

1
2
3
4
5
6
7
8
9
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文件操作

标签:

原文地址:http://www.cnblogs.com/Kingway-Python/p/5840561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!