Python 中对文件的输入输出

 1 #! /usr/bin/python
 2 # Filename:using_file.py
 3 
 4 poem = '''\
 5 Programming is fun
 6 When the work is done
 7 if you wanna make your work also fun:
 8     use Pyhon!
 9 '''
10 
11 f = file('poem.txt', 'w') # open for 'w'riting
12 f.write(poem) # write text to file
13 f.close() # close the file
14 
15 f = file('poem.txt')
16 # if no mode is sepecified, 'r'ead mode is assumed by default
17 while True:
18     line = f.readline()
19     if len(line) == 0: # zero length indecates eof
20         break
21     print line,
22     # notice comma to avoid automatic newline added by python
23 
24 f.close() # close the file
View Code
posted @ 2013-09-25 23:51  冷冰若水  阅读(171)  评论(0编辑  收藏  举报