zoukankan      html  css  js  c++  java
  • 核心编程答案(第三章)

    3-10

    #!/usr/bin/env python
    # encoding: utf-8
    
    import os
    ls = os.linesep
    fname = raw_input('Enter file name: ')
    
    all = []
    
    try:
        fobj = open(fname, 'r')
        print "'%s already exists'" % fname
    except IOError, e:
        print "
    Enter lines ('.' by itself to quite). 
    "
        while True:
            entry = raw_input('>')
            if entry == '.':
                break
            else:
                all.append(entry)
        fobj = open(fname, 'w')
        fobj.writelines('%s%s' % (x, ls) for x in all)
        fobj.close()
        print "Done!"
    #!/usr/bin/env python
    # encoding: utf-8
    import os
    'readTextFile.py -- read and display text file'
    
    fname = raw_input('Enter filename: ')
    print
    
    if os.path.exists(fname):
        fobj = open(fname, 'r')
        for eachline in fobj:
            print eachline,
        fobj.close()
    else:
        print "file open Error! "

     3-11 srtip()是将最两边全部匹配的给删除,参数为空时删除空白空间

    #!/usr/bin/env python
    # encoding: utf-8
    import os
    'readTextFile.py -- read and display text file'
    
    fname = raw_input('Enter filename: ')
    print
    
    if os.path.exists(fname):
        fobj = open(fname, 'r')
        for eachline in fobj:
            print eachline.strip('
    ')
        fobj.close()
    else:
        print "file open Error! "

     3-12

    #!/usr/bin/env python
    # encoding: utf-8
    
    import os
    ls = os.linesep
    
    choice = raw_input("What do you want to do? '1' mean write and '2' mean read: ")
    if choice == '1':
        fname = raw_input('Enter file name: ')
        all = []
        try:
            fobj = open(fname, 'r')
            print "'%s already exists'" % fname
        except IOError, e:
            print "
    Enter lines ('.' by itself to quite). 
    "
            while True:
                entry = raw_input('>')
                if entry == '.':
                    break
                else:
                    all.append(entry)
            fobj = open(fname, 'w')
            fobj.writelines('%s%s' % (x, ls) for x in all)
            fobj.close()
            print "Done!"
    elif choice == '2':
        fname = raw_input('Enter filename: ')
        print
    
        if os.path.exists(fname):
            fobj = open(fname, 'r')
            for eachline in fobj:
                print eachline.strip('
    ')
            fobj.close()
        else:
            print "file open Error! "
    else:
        print "you don't put the right number!"
  • 相关阅读:
    【win7】安装开发环境
    【php-fpm】启动PHP报错ERROR: [pool www] cannot get uid for user 'apache'
    【apache2】AH00543: httpd: bad user name apache
    【gedit】 显示行号
    关于golang.org/x包问题
    国内的go get问题的解决
    php7函数,声明,返回值等新特性介绍
    php5.6.x到php7.0.x特性
    PHP5.4.0新特性研究
    【git】如何去解决fatal: refusing to merge unrelated histories
  • 原文地址:https://www.cnblogs.com/ohmydenzi/p/5451419.html
Copyright © 2011-2022 走看看