孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

创建和读出文件p2.py和p3.py

#!/usr/bin/python
import os
ls=os.linesep
fname = raw_input('Enter filename: ')
print

while True:
        if os.path.exists(fname):
                print "Error '%s' already exists" % fname
        else:
                break

all=[]
print "\nEnter lines ('.') by itself to quit).\n"

while True:
        entry=raw_input('>')
        if entry=='.':
                break
        else:
                all.append(entry)

fobj=open(fname,'w')
执行python p2.py


读出文件
#!/usr/bin/env Python
'readTextFile.py -- read and display text file'
# get filename
fname = raw_input('Enter filename: ')
print
# attempt to open file for reading
try:
fobj = open(fname, 'r')
except IOError, e:
print "*** file open error:", e
else:
# display contents to the screen
for eachLine in fobj:
print eachLine,
fobj.close()

执行python p3.py                                                             

posted on 2011-04-23 22:40  孤独的猫  阅读(296)  评论(0编辑  收藏  举报