2013-02-24 62 views
0

這是家庭作業,我對Python很新。我寫了一個文本編輯器程序,它使用了一個基於雙線的遊標列表類。我讓用戶打開要編輯的現有文件或新文件,併爲該文件創建了一個基於光標的列表。最後,我希望用戶能夠保存所做的任何更改。我收到一些錯誤,不知道該怎麼辦。任何建議表示讚賞!python 3:從基於光標的列表寫入文件

from cursor_based_list import CursorBasedList 
from os.path import exists 
import os 

def main(): 
    def seeFile(): 
     """Asks the user if they want to view their file after editing, and 
      if yes, prints the file without None, front, or rear.""" 
     seeFile = input("Would you like to see your file now? Y/N: ").upper() 
     while not seeFile == "Y" and not seeFile == "N": 
      print("That is not a valid choice!") 
      seeFile = input("Would you like to see your file now? Y/N: ").upper() 
     if seeFile == "Y": 
      print() 
      print(fileList) 

    print() 
    print("---------------------------------------------------------------------") 
    print("Welcome to TextEd v.1, a friendly text editor program!") 
    print() 
    print("How would you like to begin?") 
    print() 
    print("O: Open an existing text file for editing") 
    print("N: Create a new text file for editing") 
    print() 
    response = input("Please choose an initial option: ").upper() 
    while response != "O" and response != "N": 
     print("That is not a valid initial option.") 
     response = input("Please choose an initial option: ").upper() 
    if response == "O": 
     fileName = input("Enter the file name: ") 
     while not exists(fileName): 
      print() 
      print("File " + fileName + " does not exist!") 
      fileName = input("Please enter a valid file name: ") 
     myFile = open(fileName, 'r') 
     data = myFile.readlines() 
     fileList = CursorBasedList() 
     for line in data: 
      fileList.insertAfter(line) 
     seeFile() 
    elif response == "N": 
     fileName = input("What would you like to name your file?: ") 
     fileList = CursorBasedList() 
     myFile = open(fileName, "w") 

    print()  
    print("TextEd Menu:") 
    print("---------------------------------------------------------------------") 
    print() 
    print("A: Insert a new line after the current line of your file") 
    print("B: Insert a new line before the current line of your file") 
    print("C: Display the current line of your file") 
    print("F: Display the first line of your file") 
    print("L: Display the last line of your file") 
    print("E: Display the next line of your file") 
    print("P: Display the previous line of your file") 
    print("D: Delete the current line of your file") 
    print("R: Replace the current line of your file with a new line") 
    print("S: Save your edited text file") 
    print("Q: Quit") 
    while True: 
     response = input("Please choose an option: ").upper() 
     if response == "A": 
      line = input("Enter the new line to insert after the current line: ") 
      line = line + "\n" 
      fileList.insertAfter(line) 
      seeFile() 
     elif response == "B": 
      line = input("Enter the new line to insert before the current line: ") 
      line = line + "\n" 
      fileList.insertBefore(line) 
      seeFile() 
     elif response == "C": 
      line = fileList.getCurrent() 
      print("The current line is:", line) 
     elif response == "F": 
      first = fileList.first() 
      print("The first line is:", fileList.getCurrent()) 
     elif response == "L": 
      last = fileList.last() 
      print("The last line is:", fileList.getCurrent()) 
     elif response == "E": 
      try: 
       nextLine = fileList.next() 
       print("The next line is:", fileList.getCurrent()) 
      except AttributeError: 
       print("You have reached the end of the file.") 
     elif response == "P": 
      try: 
       prevLine = fileList.previous() 
       print("The previous line is:", fileList.getCurrent()) 
      except AttributeError: 
       print("You have reached the beginning of the file.") 
     elif response == "D": 
      fileList.remove() 
      seeFile() 
     elif response == "R": 
      item = input("Enter the line you would like put into the file: ") 
      item = item + "\n" 
      fileList.replace(item) 
      seeFile() 
     elif response == "S": 
      temp = fileList.first() 
      while temp!= None: 
       result = str(temp.getData()) 
       myFile.write(result) 
       temp = temp.getNext() 
      myFile.close() 
      print("Your file has been saved.") 
      print() 
     elif response == "Q": 
      print("Thank you for using TextEd!") 
      break 
     else: 
      print("That is not a valid option.") 

main() 

除保存以外,一切都工作正常。還有一點需要指出的是,當我到達myFile.close()時,出現錯誤,提示「列表對象沒有關閉屬性」。

如果您想查看更多的代碼,請告訴我!我知道這可能不是「完美」的代碼,所以請耐心等待。謝謝!

elif response == "S": 
      myFile = open(fileName,"w") 
      fileList.first() 
      current = fileList.getCurrent() 
      try: 
       for x in range(len(fileList)): 
        myFile.write(str(current)) 
        current = fileList.next() 
        current = fileList.getCurrent() 
        print(current) 
      except AttributeError: 
       myFile.close() 
       print("Your file has been saved.") 
       print() 

好吧,我終於得到它與上述代碼一起工作。我相信這可能是寫它的最醜陋的方式,但至少它是有效的!

回答

1

首先,你在這裏分配myFile

 myFile = open(fileName, 'r') 

當時,MYFILE是一個文件對象。但是,那麼你這樣做:

 myFile = myFile.readlines() 

現在MYFILE是一個包含文件中的所有行的列表,因此不能再被關閉。將myFile.readlines()分配給一個不同的變量,你會沒事的。

有關文件輸入/輸出,請參閱the documentation

fileList也是在寫作的時候空的,因爲當你打開文件寫您還可以設置fileList到一個新的CursorBasedList這裏:

elif response == "N": 
     fileName = input("What would you like to name your file?: ") 
     fileList = CursorBasedList() # <- Here 
     myFile = open(fileName, "w") 

如果去掉那行,它應該工作的罰款。

+0

感謝您的提示!我做了這個改變,它反映在我上面的代碼中。我不再收到錯誤,但是當我保存時,實際上沒有任何內容保存到文件中。我仍然有原始文件,或者如果我創建了一個新文件,則會有一個空白文件。任何猜測? :) – AbigailB 2013-02-24 15:58:45

+0

我試圖添加一個新的命令來打開文件進行寫入,但這似乎沒有幫助。當我在現有文件上執行該操作時,它會像我期望的那樣刪除內容,但不會將新列表寫入文件。 – AbigailB 2013-02-24 16:03:45

+0

修改了我之前的回答,參見上文。 – 2013-02-24 16:09:10