python file

时间:2022-04-07 15:26:36
python file
 >>> help(open)
Help on built-in function open in module __builtin__: open(...)
open(name[, mode[, buffering]]) -> file object Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information. >>> f=open('E:\\tmp.txt')
>>> f
<open file 'E:\\tmp.txt', mode 'r' at 0x0000000002F84540>
>>> f.read()
'nihao\nwozhidao\n\xc4\xb2\xb0\xd8\xd0\xf1\n'
>>> list(f)
[]
>>> f.read(5)
''
>>> f.read()
''
>>> f=open("E:\\tmp.txt")
>>> f.read
<built-in method read of file object at 0x0000000002F845D0>
>>> f.read()
'nihao\nwozhidao\n\xc4\xb2\xb0\xd8\xd0\xf1\n'
>>> f.read()
''
>>> f=open("E:\\tmp.txt")
>>> f
<open file 'E:\\tmp.txt', mode 'r' at 0x0000000002F84540>
>>> f.read(5)
'nihao'
>>> f.seek() Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
f.seek()
TypeError: seek() takes at least 1 argument (0 given)
>>> f.tell()
5L
>>> lines=list(f)
>>> for eachLine in lines:
print eachLine wozhidao 牟柏旭 >>> f
<open file 'E:\\tmp.txt', mode 'r' at 0x0000000002F84540>
>>> f.read()
''
>>> f.seek(0,0)
>>> f.readline()
'nihao\n'
>>> f.seek(0,0)
>>> fw=open('E:\\tmp.txt','w')
>>> fw.write('let's go') SyntaxError: invalid syntax
>>> fr=open('E:\\tmp1.txt','w')
>>> fr.write('nihao')
>>> fr.close()

python file