zoukankan      html  css  js  c++  java
  • 创建文件

    创建和读出文件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                                                             

  • 相关阅读:
    【转】C++11优化使用emplace,emplace_back
    面试经历总结
    Hive常用函数
    股票指标
    Visual Studio Code 可以翻盘成功主要是因为什么?
    openpyxl模块操作Excel
    JavaScript(二)
    前端之CSS
    ps导出ICO格式
    Qt 所有版本官方下载地址
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2025922.html
Copyright © 2011-2022 走看看