Python读文件
时间:2014-06-06 10:01:46
收藏:0
阅读:261
读取文件第一行内容:
|
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# ls1.txt
4.py 7.py
9.py
ac.tar aobama newfile2.txt
5.py 8.py
aa.tar.gz ac.tar.bz2 evilxr3.py
6.py 8.py~ ab.tar.bz2 ac.tar.gz evilxr.txtroot@IdeaPad:~/test# cat evilxr.txt s1 =
‘www.evilxr.com‘ev =
[1,3,4,5,6,7,86,120]i =
0for
c in
s1: print
format (i,‘2d‘), c i =
i +
1else: print
‘The for is out!‘i =
0for
evilxr in
ev: print
format(i,‘2d‘),evilxr i =
i +
1print
‘This is New!‘tup =
(1,2,3,4,5)for
t in
tup: print
telse: print
‘This is tup!‘root@IdeaPad:~/test# |
评论(0)