读取文件第一行内容:
1
2
3
4 |
for
c in
open ( ‘7.py‘ , ‘r‘ ).readline(): print
c, else : print
‘readline is over!‘ |
打印结果为:
1
2
3 |
root@IdeaPad:~ / test # python 8.py s 1
= ‘ w w w . e v i l x r . c o m ‘ readline is
over! |
用Python实现文件内容的拷贝:
1
2
3 |
root@IdeaPad:~ / test # cat 9.py for
ss in
open ( ‘7.py‘ , ‘r‘ ).readlines(): open ( ‘evilxr.txt‘ , ‘a+‘ ).write(ss) |
运行该python后会在该目录下生成一个名为evilxr.txt的文件,该文件的内容和7.py的一样:
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 |
root@IdeaPad:~ / test # ls 1.txt
4.py 7.py
9.py
ac.tar aobama newfile 2.txt
5.py 8.py
aa.tar.gz ac.tar.bz2 evilxr 3.py
6.py 8.py ~ ab.tar.bz2 ac.tar.gz evilxr.txt root@IdeaPad:~ / test # cat evilxr.txt s1 =
‘www.evilxr.com‘ ev =
[ 1 , 3 , 4 , 5 , 6 , 7 , 86 , 120 ] i =
0 for
c in
s1: print
format (i, ‘2d‘ ), c i =
i +
1 else : print
‘The for is out!‘ i =
0 for
evilxr in
ev: print
format (i, ‘2d‘ ),evilxr i =
i +
1 print
‘This is New!‘ tup =
( 1 , 2 , 3 , 4 , 5 ) for
t in
tup: print
t else : print
‘This is tup!‘ root@IdeaPad:~ / test # |
原文地址:http://www.cnblogs.com/evilxr/p/3767351.html