1. 程式人生 > >零基礎學python-3.7 還有一個程序 python讀寫文本

零基礎學python-3.7 還有一個程序 python讀寫文本

efi == put ret mode nbsp inpu exce for each

今天我們引入另外一個程序,文件的讀寫

我們先把簡單的程序代碼貼上。然後通過我們多次的改進。希望最後可以變成一個簡單的文本編輯器

以下是我們最簡單的代碼:

‘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讀寫文本