Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1695808
  • 博文数量: 362
  • 博客积分: 10587
  • 博客等级: 上将
  • 技术积分: 4098
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-10 18:15
文章分类

全部博文(362)

文章存档

2014年(1)

2013年(58)

2011年(115)

2010年(112)

2009年(76)

分类:

2009-10-29 16:14:54

"This is a test module"

import sys
import os
ls = os.linesep

def make():
    'maketextfile.py --- create text file'

    # get filename
    while True:
   
        if os.path.exists('C:\Python26\make.log'):
            print "ERROR: '%s' already exists" % ('C:\Python26\make.log')
        else:
            break
       
    # get file content (text) lines
    all=[]
    print"\nEnter lines ('.'by itselef to quit).\n"

    # loop until user terminates input
    while True:
        entry = raw_input('>')
        if entry == '.':
            break
        else:
            all.append(entry)

    # write lines to file with proper line-ending
    fobj = open(r'C:\Python26\make.log','w')
    fobj.writelines(['%s%s' % (x,ls) for x in all])
    fobj.close()
    print 'DONE!'

 


def read():
    '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()

if __name__ == '__main__':
    make()
    read()


 

阅读(2825) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~