File Input output D:/QiBaoyuan/Python/IO>python Python 3.0rc3 (r30rc3:67313, Nov 21 2008, 07:14:45) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> f=read('test.txt','r+') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'read' is not defined >>> f=read('test.txt','r') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'read' is not defined >>> f=open('test.txt','r') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:/Python30/lib/io.py", line 278, in __new__ return open(*args, **kwargs) File "C:/Python30/lib/io.py", line 222, in open closefd) File "C:/Python30/lib/io.py", line 615, in __init__ _fileio._FileIO.__init__(self, name, mode, closefd) IOError: [Errno 2] No such file or directory: 'test.txt' >>> f=open('test.txt','r') >>> f.read <bound method TextIOWrapper.read of <io.TextIOWrapper object at 0x00BF4630>> >>> f.read() 'Hello this is plain-text~~~' >>> f.read() '' >>> f.readline() '' >>> f=open('test.txt','r') >>> f.readline() 'Hello this is plain-text~~~' >>> f.readline() '/n' >>> f=open('test.txt','r') >>> f.readlien() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'TextIOWrapper' object has no attribute 'readlien' >>> f.readline() 'Hello this is plain-text~~~/n' >>> f.readline() '我爱北京天门' >>> f.readline() '' >>> f.readline() '' >>> f=open('test.txt','r') >>> f.readlines() ['Hello this is plain-text~~~/n', '我爱北京天门'] >>> f=open('test.txt','r') >>> for line in f: ... print(line,end=' ') ... Hello this is plain-text~~~ 我爱北京天门 >>> for line in f: ... File "<stdin>", line 2 ^ IndentationError: expected an indented block >>> for line in f: ... print(line,end=' hdfdhsfhk ') ... >>> >>> >>> >>> >>> >>> >>> >>> f=open('test.txt','r') >>> for line in f: ... print(line,end='hhh') ... Hello this is plain-text~~~ hhh我爱北京天门hhh>>> >>> f.write('咔咔/n') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:/Python30/lib/io.py", line 1492, in write self.buffer.write(b) File "C:/Python30/lib/io.py", line 696, in write self._unsupported("write") File "C:/Python30/lib/io.py", line 322, in _unsupported (self.__class__.__name__, name)) io.UnsupportedOperation: BufferedReader.write() not supported >>> f=open('test.txt','r+') >>> f.write('咔咔/n') 3 >>> f.readlines() ['this is plain-text~~~/n', '我爱北京天门'] >>> f=open('test.txt','r') >>> f <io.TextIOWrapper object at 0x00BF4910> >>> f.readlines() ['咔咔/n', 'this is plain-text~~~/n', '我爱北京天门'] >>> f=open('test.txt','r') >>> f.read() '咔咔/nthis is plain-text~~~/n我爱北京天门' >>> value=('the answer',42) >>> s=str(value) >>> s "('the answer', 42)" >>> f=open('test.txt','r+') >>> f.write(s) 18 >>> f=open('test.txt','r+') >>> f.read() "('the answer', 42)n-text~~~/n我爱北京天门" >>> f=open('test2.txt','rb+') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:/Python30/lib/io.py", line 278, in __new__ return open(*args, **kwargs) File "C:/Python30/lib/io.py", line 222, in open closefd) File "C:/Python30/lib/io.py", line 615, in __init__ _fileio._FileIO.__init__(self, name, mode, closefd) IOError: [Errno 2] No such file or directory: 'test2.txt' >>> f=open('test2.txt','rb+') >>> f.write(b'0123456789abfasfdgfsdj') 22 >>> f.seek(5) 5 >>> f.read(1) b'5' >>> f.seek(-3) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:/Python30/lib/io.py", line 1183, in seek pos = self.raw.seek(pos, whence) IOError: [Errno 22] Invalid argument >>> f.seek(-3,2) 19 >>> f.read(1) b's' >>> f.close() >>> f.read() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:/Python30/lib/io.py", line 1204, in read self.flush() File "C:/Python30/lib/io.py", line 1071, in flush self._flush_unlocked() File "C:/Python30/lib/io.py", line 1075, in _flush_unlocked raise ValueError("flush of closed file") ValueError: flush of closed file >>> with open('test2.txt','r') as f: ... read_data=f.read() ... >>> f.close() >>> f.closed True >>> class t: ... pass ... >>> f=open('test2.txt','r+') >>> pickle.dump(t,f) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'pickle' is not defined >>> import sys >>> pickle.dump(t,f) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'pickle' is not defined >>>