python 文件打开和关闭

[user1@localhost ~]$ ls
Desktop    Downloads  Pictures  Templates  test.txt~
Documents  Music      Public    test.txt   Videos
[user1@localhost ~]$ cat test.txt
www.cvst.net
i am a python leaner.
hello world.
[user1@localhost ~]$ python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fo = open('test.txt')
>>> fo

>>> fo.read()
'www.cvst.net\ni am a python leaner.\nhello world.\n'
>>> fo.close()
>>> fo.read()
Traceback (most recent call last):
  File "", line 1, in 
ValueError: I/O operation on closed file
>>> f1 = file('test.txt')
>>> f1.read()
'www.cvst.net\ni am a python leaner.\nhello world.\n'
>>> f1.close()
>>> f1.read()
Traceback (most recent call last):
  File "", line 1, in 
ValueError: I/O operation on closed file
>>> f1 = open('test.txt')
>>> f1

>>> f1.write('good csvt')
Traceback (most recent call last):
  File "", line 1, in 
IOError: File not open for writing
>>> f1.close

>>> 

你可能感兴趣的:(Python,python视频学习)