python基础 十一 文件操作
1.打开文件
|
1
2
3
|
使用open()文件句柄 = open('文件路径', '模式') |
2.文件操作
|
1
|
打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。 |
打开文件的模式有:
|
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
r ,只读模式【默认模式,文件必须存在,不存在则抛出异常】w,只写模式【不可读;不存在则创建;存在则清空内容】x, 只写模式【不可读;不存在则创建,存在则报错】a, 追加模式【可读; 不存在则创建;存在则只追加内容】 "+" 表示可以同时读写某个文件 r+, 读写【可读,可写】w+,写读【可读,可写】x+ ,写读【可读,可写】a+, 写读【可读,可写】 "b"表示以字节的方式操作 rb 或 r+bwb 或 w+bxb 或 w+bab 或 a+b 注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型,不能指定编码 3.关闭文件 使用close()补充:with语句自带使用close()方法, 这也是为了防止打开文件忘记关闭比如:with open() as f_read:#打开文件是不需要添加close()的。 |
二.文件操作
|
1
2
3
4
5
6
7
8
9
10
11
|
1.read文件名.read(n):读取指定个数的字符,文件从光标位置开始注意:python3版本读取的是字符 python2版本读取的是字节在读取完第一次字符时,第二次读取从上次读取完的字符开始比如: |
1 f = open("test",mode="r",encoding="utf8")
2
3 data = f.read(5)
4 data2 = f.read(6)
5 print(data)
6 print(data2)
7 得到的答案是
8 globa
9 l
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
f.readline 读取一行,光标到下一行行首f.readlines 显示列表结果,每一个元素是一行内容行与字符串的拼接使用join用法文件名.join([文件名,“需要添加的字符串”])循环整个文件,并在一行中拼接使用for循环2.writef =open(name,mode=w) 修改文件可读权限文件名.write("") 覆盖文件信息文件名.write("\n") 换行f =open(name,mode="a")追加信息,原来文件信息不变f =open(name,mode="x")原有的文件不覆盖,会报错;没有的文件重新创建f=write("")把内容添加到内存,在一定时间内显示出来f.writelines(["11\n","22\n"])写多行f.truncate() 截断 只能在写权限操作 |
3.可读可写模式:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
r+ 追加写 默认光标在开始位置w+ 首先会覆盖,清空,然后再写,seek调整a+ 总是在最后位置添加。光标在文件最后位置 无论光标在什么位置,一定是追加写: 想读取内容:seek调增4.flush操作f=flush("") 直接显示,内容直接存储在硬盘上比如:进度条操作 |
1 sys.stdout.write("")相当于print
2 sys.stdout 文件
3 循环打印#
4 import sys
5 for i in range(10):
6 sys.stdout.write("#")
7 sys.stdout.flush()
8 import time
9 time.sleep(0.5)
|
1
2
3
4
5
6
7
8
|
显示百分比1 import sys2 for i in range(6):3 s = "\r%s%% %s"%(i,"?"*i)4 sys.stdout.write(s)5 sys.stdout.flush()6 import time7 time.sleep(0.5) |
四、读取的三个方式
|
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
文本文件:hello world! this is a test text read():从当前位置开始,读完所有的文件,返回字符串。>>> myfile = open("test.txt","r") >>> myfile.read() 'hello\nworld!\nthis \nis \na \ntest\ntext\n' >>> myfile.read() '' [python] view plain copy>>> myfile.close() readline():只读下一行内容,返回字符串。>>> myfile = open("test.txt","r") >>> myfile.readline() 'hello\n' >>> myfile.readline() 'world!\n' >>> myfile.readline() 'this \n' >>> myfile.readlines() ['is \n', 'a \n', 'test\n', 'text\n'] >>> myfile.close() readlines():从当前位置,每次读取多行,返回列表。>>> myfile = open("test.txt","r") >>> myfile.readlines() ['hello\n', 'world!\n', 'this \n', 'is \n', 'a \n', 'test\n', 'text\n'] >>> myfile.readline() '' >>> myfile.close() |
五、其他操作
tell和seek
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
tell:查询文件中光标位置 seek:光标定位f = open('file','r')print(f.tell()) #光标默认在起始位置f.seek(10) #把光标定位到第10个字符之后print(f.tell()) #输出10f.close()----------------------f = open('file','w')print(f.tell()) #先清空内容,光标回到0位置f.seek(10) print(f.tell())f.close()----------------------f = open('file','a')print(f.tell()) #光标默认在最后位置f.write('你好 世界')print(f.tell()) #光标向后9个字符,仍在最后位置f.close() |
flush 同步将数据从缓存转移到磁盘
|
1
2
3
4
5
6
7
8
9
10
11
12
|
示例,实现进度条功能import sys,time #导入sys和time模块for i in range(40): sys.stdout.write('*') sys.stdout.flush() #flush的作用相当于照相,拍一张冲洗一张 time.sleep(0.2)下面代码也能够实现相同的功能import timefor i in range(40): print('*',end='',flush=True) #print中的flush参数 time.sleep(0.2) |
truncate 截断
|
1
2
3
4
5
6
7
8
|
不能是r模式下执行,w模式下,已经清空所有数据,使用truncate没有任何意义,a模式下,截断指定位置后的内容。 f = open('file','a') f.truncate(6) #只显示6个字节的内容(6个英文字符或三个汉字),后面的内容被清空。 |

浙公网安备 33010602011771号