这是作业,我对 Python 还是很陌生。我编写了一个文本编辑器程序,它使用基于双行光标的列表类。我让用户打开要编辑的现有文件或新文件,并为该文件创建了一个基于光标的列表。最后,我希望用户能够将他们所做的任何更改保存回文件。我收到一些错误,不知道该怎么做。任何建议表示赞赏!
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()
好的,我终于让它与上面的代码一起工作了。我敢肯定这可能是最丑陋的写法,但至少它有效!