说明:python读取文件,需设置文件为一个变量,然后读取
|
模式 |
描述 |
|
r |
以读方式打开文件,可读取文件信息。 |
|
w |
以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容 |
|
a |
以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建 |
|
r+ |
以读写方式打开文件,可对文件进行读和写操作。 |
|
w+ |
消除文件内容,然后以读写方式打开文件。 |
|
a+ |
以读写方式打开文件,并把文件指针移到文件尾。 |
|
b |
以二进制模式打开文件,而不是以文本模式。该模式只对Windows或Dos有效,类Unix的文件是用二进制模式进行操作的。 |
一、文件的打开和创建
>>> f = open('/tmp/test.txt')
>>> f = file('/tmp/test.txt') ##f = open('/tmp/test.txt')相同(python3弃用file)
>>> f.read()
>>> f
<open file '/tmp/test.txt', mode 'r' at 0x7f5285181d20> #0x7f5285181d20 内存指令
'hello girl
hello boy!
hello man!
hello man!
hello dear!
hello bady!
hello lady!'
二、文件的读取
步骤:打开 -- 读取 --刷新-- 关闭
>>> f = file('/tmp/test.txt','r')
>>> f.read() ##方法一:读取所有内容
'hello girl
hello boy!
hello man!
hello man!
hello dear!
hello bady!
hello lady!'
>>> f.readline() ##方法二:逐行读取数据
'hello girl
'
>>> f.next() ##方法三:逐行读取数据,读取最后一行后,无内容报错
'hello lady!'
>>> f.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> f.readlines() # 方法四:将文件内容以列表的形式存放
['hello boy!
', '
', 'hello man!
', 'hello man!
', 'hello dear!
', 'hello bady!
', 'hello lady!']
>>> for i in open('/tmp/test.txt'): ##方法五:for循环,打印
... print i
...
hello python!
hello shell!
hello world!
三、文件的写入,关闭(慎重,小心别清空原本的文件)
>>> f = file('/tmp/test.txt','w')
>>> f.write('hello python!
')
>>> f.flush()
[root@demo_host1 tmp]# cat test.txt
hello python!
hello python!
使用r+ 模式不会先清空,但是会替换掉原先的文件
>>> f = file('/tmp/test.txt','r+')
>>> f.write('
hello shell!')
>>> f.flush()
[root@demo_host1 tmp]# cat test.txt
hello shell!
hello shell!
实现不替换
①使用r+ 模式
>>> f = file('/tmp/test.txt','r+')
>>> f.read()
'hello python!
'
>>> f.write('hello shell!
')
>>> f.flush()
②使用a模式打开文件
>>> f = file('/tmp/test.txt','a')
>>> f.write('hello world!
')
>>> f.flush()
f.writelines() 多行写入
|
1
2
3
4
5
6
7
8
9
10
11
|
>>> l = ['
hello dear!','
hello son!','
hello baby!
']>>> f = open('/tmp/test.txt','a')>>> f.writelines(l)>>> f.close()[root@node1 python]# cat /tmp/test.txthello girl!hello boy!hello man!hello dear!hello son!hello baby! |
可以看到,如果在写之前先读取一下文件,再进行写入,则写入的数据会添加到文件末尾而不会替换掉原先的文件。这是因为指针引起的,r+ 模式的指针默认是在文件的开头,如果直接写入,则会覆盖源文件,通过read() 读取文件后,指针会移到文件的末尾,再写入数据就不会有问题了。这里也可以使用a 模式
f.seek(偏移量,选项)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
>>> f = open('/tmp/test.txt','r+')>>> f.readline()'hello girl!
'>>> f.readline()'hello boy!
'>>> f.readline()'hello man!
'>>> f.readline()' '>>> f.close()>>> f = open('/tmp/test.txt','r+')>>> f.read()'hello girl!
hello boy!
hello man!
'>>> f.readline()''>>> f.close() |
这个例子可以充分的解释前面使用r+这个模式的时候,为什么需要执行f.read()之后才能正常插入
f.seek(偏移量,选项)
选项=0,表示将文件指针指向从文件头部到“偏移量”字节处
选项=1,表示将文件指针指向从文件的当前位置,向后移动“偏移量”字节
选项=2,表示将文件指针指向从文件的尾部,向前移动“偏移量”字节
偏移量:正数表示向右偏移,负数表示向左偏移
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> f = open('/tmp/test.txt','r+')>>> f.seek(0,2)>>> f.readline()''>>> f.seek(0,0)>>> f.readline()'hello girl!
'>>> f.readline()'hello boy!
'>>> f.readline()'hello man!
'>>> f.readline()'' |
f.tell() 获取指针位置
|
1
2
3
4
5
6
7
8
9
|
>>> f = open('/tmp/test.txt')>>> f.readline()'hello girl!
'>>> f.tell()12>>> f.readline()'hello boy!
'>>> f.tell()23
|