码迷,mamicode.com
首页 > 编程语言 > 详细

零基础学python-3.7 另一个程序

时间:2015-08-16 00:41:48      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:python   文本编辑   

今天我们引入另外一个程序,文件的读写

我们先把简单的程序代码贴上,然后通过我们多次的改进,希望最后能够变成一个简单的文本编辑器

下面是我们最简单的代码:

'crudfile--读写文件'
def readWholeFile(fileName):
    '读取整个文件'
    file = open(fileName, mode='r')
    text = []
    for eachLine in file:
        print(eachLine)
        text.append(eachLine)
    return text

def writeFile(fileName):
    '写文件'
    handler = open(fileName, mode='w')
    while True:
        inputText = input('>')
        if inputText == 'exit':
            break
        else:
            handler.write(inputText) 
try:
    fileName = "test.txt"
    writeFile(fileName);
    textInFile = readWholeFile(fileName);
except IOError as e:
    print(e)
    print('文件不存在')


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

零基础学python-3.7 另一个程序

标签:python   文本编辑   

原文地址:http://blog.csdn.net/raylee2007/article/details/47686489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!