1
2
3
4
5
|
>>> f = open ( '/tmp/test.txt' )
>>> f.read()
'hello python!\nhello world!\n'
>>> f
< open file '/tmp/test.txt' , mode 'r' at 0x7fb2255efc00 >
|
1
2
3
4
|
>>> f = open ( '/tmp/test.txt' )
>>> f.read()
'hello python!\nhello world!\n'
>>> f.close()
|
步骤:打开 -- 写入 -- (保存)关闭
1
2
3
4
5
6
|
>>> f.write( 'hello boy' )
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
IOError: File not open for writing
>>> f
< open file '/tmp/test.txt' , mode 'r' at 0x7fe550a49d20 >
|
1
2
|
>>> f1 = open ( '/tmp/test.txt' , 'w' )
>>> f1.write( 'hello boy!' )
|
1
2
|
[root@node1 ~] # cat /tmp/test.txt
[root@node1 ~] #
|
1
2
3
|
>>> f1.close()
[root@node1 ~] # cat /tmp/test.txt
[root@node1 ~] # hello boy!
|
注意:这一步需要相当慎重,因为如果编辑的文件存在的话,这一步操作会先清空这个文件再重新写入。那么如果不要清空文件再写入该如何做呢?
使用r+ 模式不会先清空,但是会替换掉原先的文件,如下面的例子:hello boy! 被替换成hello aay!
1
2
3
4
5
|
>>> f2 = open ( '/tmp/test.txt' , 'r+' )
>>> f2.write( '\nhello aa!' )
>>> f2.close()
[root@node1 python] # cat /tmp/test.txt
hello aay!
|
1
2
3
4
5
6
7
8
|
>>> f2 = open ( '/tmp/test.txt' , 'r+' )
>>> f2.read()
'hello girl!'
>>> f2.write( '\nhello boy!' )
>>> f2.close()
[root@node1 python] # cat /tmp/test.txt
hello girl!
hello boy!
|
1
2
3
4
5
6
7
8
|
>>> f = open ( '/tmp/test.txt' , 'a' )
>>> f.write( '\nhello man!' )
>>> f.close()
>>>
[root@node1 python] # cat /tmp/test.txt
hello girl!
hello boy!
hello man!
|
以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建 | |
文件对象的方法:
f.readline() 逐行读取数据
方法一:
1
2
3
4
5
6
7
8
9
|
>>> f = open ( '/tmp/test.txt' )
>>> f.readline()
'hello girl!\n'
>>> f.readline()
'hello boy!\n'
>>> f.readline()
'hello man!'
>>> f.readline()
''
|
1
2
3
4
5
6
|
>>> for i in open ( '/tmp/test.txt' ):
... print i
...
hello girl!
hello boy!
hello man!
|
1
2
3
4
|
>>> f = open ( '/tmp/test.txt' )
>>> f.readlines()
[ 'hello girl!\n' , 'hello boy!\n' , 'hello man!' ]
>>> f.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
>>> f = open ( '/tmp/test.txt' )
>>> f.readlines()
[ 'hello girl!\n' , 'hello boy!\n' , 'hello man!' ]
>>> f.close()
>>>
>>> f = open ( '/tmp/test.txt' )
>>> f. next ()
'hello girl!\n'
>>> f. next ()
'hello boy!\n'
>>> f. next ()
'hello man!'
>>> f. next ()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
StopIteration
|
1
2
3
4
5
6
7
8
9
10
11
|
>>> l = [ '\nhello dear!' , '\nhello son!' , '\nhello baby!\n' ]
>>> f = open ( '/tmp/test.txt' , 'a' )
>>> f.writelines(l)
>>> f.close()
[root@node1 python] # cat /tmp/test.txt
hello girl!
hello boy!
hello man!
hello dear!
hello son!
hello baby!
|
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!\n'
>>> f.readline()
'hello boy!\n'
>>> f.readline()
'hello man!\n'
>>> f.readline()
' '
>>> f.close()
>>> f = open ( '/tmp/test.txt' , 'r+' )
>>> f.read()
'hello girl!\nhello boy!\nhello man!\n'
>>> 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!\n'
>>> f.readline()
'hello boy!\n'
>>> f.readline()
'hello man!\n'
>>> f.readline()
''
|
1
2
3
4
5
6
7
|
>>> f.write( 'hello python!' )
>>> f.flush()
[root@node1 python] # cat /tmp/test.txt
hello girl!
hello boy!
hello man!
hello python!
|
1
2
3
4
5
6
7
8
9
|
>>> f = open ( '/tmp/test.txt' )
>>> f.readline()
'hello girl!\n'
>>> f.tell()
12
>>> f.readline()
'hello boy!\n'
>>> f.tell()
23
|
1
2
3
4
5
|
[root@node1 ~] # cat /tmp/test.txt
hello girl!
hello boy!
hello man!
hello python!
|
脚本如下:
方法一:
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/python
import re
f = open ( '/tmp/test.txt' )
source = f.read()
f.close()
r = r 'hello'
s = len (re.findall(r,source))
print s
[root@node1 python] # python count.py
4
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/python
import re
fp = file ( "/tmp/test.txt" , 'r' )
count = 0
for s in fp.readlines():
li = re.findall( "hello" ,s)
if len (li)> 0 :
count = count + len (li)
print "Search" ,count, "hello"
fp.close()
[root@node1 python] # python count1.py
Search 4 hello
|
二、替换
实例:把test.txt 中的hello全部换为"hi",并把结果保存到myhello.txt中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/python
import re
f1 = open ( '/tmp/test.txt' )
f2 = open ( '/tmp/myhello.txt' , 'r+' )
for s in f1.readlines():
f2.write(s.replace( 'hello' , 'hi' ))
f1.close()
f2.close()
[root@node1 python] # touch /tmp/myhello.txt
[root@node1 ~] # cat /tmp/myhello.txt
hi girl!
hi boy!
hi man!
hi python!
|
#some words
Sometimes in life,
You find a special friend;
Someone who changes your life just by being part of it.
Someone who makes you laugh until you can't stop;
Someone who makes you believe that there really is good in the world.
Someone who convinces you that there really is an unlocked door just waiting for you to open it.
This is Forever Friendship.
when you're down,
and the world seems dark and empty,
Your forever friend lifts you up in spirits and makes that dark and empty world
suddenly seem bright and full.
Your forever friend gets you through the hard times,the sad times,and the confused times.
If you turn and walk away,
Your forever friend follows,
If you lose you way,
Your forever friend guides you and cheers you on.
Your forever friend holds your hand and tells you that everything is going to be okay.
1
2
3
4
5
6
7
8
9
10
|
f = open ( 'cdays-4-test.txt' )
result = list ()
for line in f.readlines(): # 逐行读取数据
line = line.strip() #去掉每行头尾空白
if not len (line) or line.startswith( '#' ): # 判断是否是空行或注释行
continue #是的话,跳过不处理
result.append(line) #保存
result.sort() #排序结果
print result
open ( 'cdays-4-result.txt' , 'w' ).write( '%s' % '\n' .join(result)) #保存入结果文件
|