python file
1 >>> help(open)2 Help on built-in function open in module __builtin__:
3
4 open(...)
5 open(name[, mode[, buffering]]) -> file object
6
7 Open a file using the file() type, returns a file object. This is the
8 preferred way to open a file. See file.__doc__ for further information.
9
10 >>> f=open('E:\\tmp.txt')
11 >>> f
12 <open file 'E:\\tmp.txt', mode 'r' at 0x0000000002F84540>
13 >>> f.read()
14 'nihao\nwozhidao\n\xc4\xb2\xb0\xd8\xd0\xf1\n'
15 >>> list(f)
16 []
17 >>> f.read(5)
18 ''
19 >>> f.read()
20 ''
21 >>> f=open("E:\\tmp.txt")
22 >>> f.read
23 <built-in method read of file object at 0x0000000002F845D0>
24 >>> f.read()
25 'nihao\nwozhidao\n\xc4\xb2\xb0\xd8\xd0\xf1\n'
26 >>> f.read()
27 ''
28 >>> f=open("E:\\tmp.txt")
29 >>> f
30 <open file 'E:\\tmp.txt', mode 'r' at 0x0000000002F84540>
31 >>> f.read(5)
32 'nihao'
33 >>> f.seek()
34
35 Traceback (most recent call last):
36 File "<pyshell#14>", line 1, in <module>
37 f.seek()
38 TypeError: seek() takes at least 1 argument (0 given)
39 >>> f.tell()
40 5L
41 >>> lines=list(f)
42 >>> for eachLine in lines:
43 print eachLine
44
45
46
47
48 wozhidao
49
50 牟柏旭
51
52 >>> f
53 <open file 'E:\\tmp.txt', mode 'r' at 0x0000000002F84540>
54 >>> f.read()
55 ''
56 >>> f.seek(0,0)
57 >>> f.readline()
58 'nihao\n'
59 >>> f.seek(0,0)
60 >>> fw=open('E:\\tmp.txt','w')
61 >>> fw.write('let's go')
62
63 SyntaxError: invalid syntax
64 >>> fr=open('E:\\tmp1.txt','w')
65 >>> fr.write('nihao')
66 >>> fr.close()
以上是 python file 的全部内容, 来源链接: utcz.com/z/388471.html