zoukankan      html  css  js  c++  java
  • python 学习源码练习(2)——简单文件读取

    #文件创建

    #!/usr/bin/python3

    'makeTextFile.py--create text file'

    import os

    ls = os.linesep

    #get filename
    fname = input('please enter the filename:')
    while True:
        if os.path.exists(fname):
            print ( "Error : '%s' already exists" % fname)
        else:
            break

    #get file content(text) lines

    all = []
    print (" Enter Lines('.' by itself to quit). ")

    #loop until user terminates input

    while True:
        entery = input('>')
        if entery == '.':
            break
        else:
            all.append(entery)

    #wirte lines in file
    fobj = open(fname, 'w')
    fobj.writelines(['%s%s' %(x, ls) for x in all])
    fobj.close()

    print ('Done!')

    #read file and output to the screen

    #!/usr/bin/python3


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

    #get file name

    fname = input('please input the file to read:')


    try:
        fobj = open(fname,'r')
    except:
        print("*** file open error" ,e)
    else:
    #display the contents of the file to the screen.

        for eachline in fobj:
            print(eachline,)
        fobj.close()

  • 相关阅读:
    View onMeasure方法介绍
    控件的3个状态
    多个Activity之间的跳转(1)
    7种形式的Android Dialog使用举例(下)
    调用摄像头采集图像
    读取播放视频
    MATLAB
    下载数据CSV文件格式
    生成数据
    java中给当前时间添加一小时
  • 原文地址:https://www.cnblogs.com/honorplus/p/7906384.html
Copyright © 2011-2022 走看看