これは宿題です。私は 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() に到達すると、「リスト オブジェクトに 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()
さて、ようやく上記のコードで動作するようになりました。これはおそらく最も醜い書き方だと思いますが、少なくともうまくいきます!