• 《Python核心编程》第二版第55页第三章练习 续一 Python核心编程答案自己做的


    下面的问题涉及了makeTextFile.py和readTextFile.py脚本。
    【注】网络上找到的makeTextFile.py和readTextFile.py源代码,和原书例子稍有区别。

    'makeTextFile.py -- create text file'

    import os

    # get filename
    while True:
        fname = raw_input('Enter file name: ')
        if os.path.exists(fname):
            print"*** ERROR: '%s' already exists" % fname
        else:
            break

    # get file content (text) lines
    all = []
    print "\nEnter lines ('.' by itself 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 NEWLINE line terminator
    fobj = open(fname, 'w')
    fobj.write('\n'.join(all))
    fobj.close()
    print 'DONE!'


    'readTextFile.py -- read and display text file'

    # get filename
    fname = raw_input('Enter file name: ')
    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()


    3-8.
    Python代码。将脚本拷贝到你的文件系统中,然后修改它。可以添加注释,修改提示符(‘>’太单调了)等,修改这些代码,使他们看上去更舒服。
    【答案】
    略。

    3-9.
    移植。如果你在不同类型的计算机系统中分别安装有Python,检查一下,os.linesep的值是否有不同。记下操作系统的类型及linesep的值。
    【答案】
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\root>python
    Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.linesep
    '\r\n'
    >>>

    在一个linux操作系统中

    [GCC 4.1.1] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.linesep
    '\n'

  • 相关阅读:
    mysql 导入报错(ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_E)
    Docker容器开机自动启动
    linux 查看内存条详情命令
    Linux 网络性能测试工具 iperf 的安装和使用
    redis基准性能测试
    pmm的安装,这里推荐下载官方提供的脚本,直接执行这个脚本就可以完成安装
    mysqlslap压力测试时出现"Can't connect to MySQL server"
    Linux监控工具介绍系列——iostat
    提高RabbitMQ的File descriptors
    python 打包
  • 原文地址:https://www.cnblogs.com/balian/p/1938016.html
Copyright © 2020-2023  润新知