python笔记本:更改写入的文件

python笔记本:更改写入的文件,python,python-3.x,Python,Python 3.x,请帮我看看这个问题。我试图提交任务的工具名为viope.com,这是一个自学环境,它在检查代码时有时会出错。我的代码在第一轮似乎运行良好,但我不明白为什么在第二轮选择1时它不打印,它似乎捕获了文件名的更改任务还有另一个持续的项目,笔记本电脑,依赖于用户的操作,如果用户决定读取文件而不向其写入任何内容,它就会崩溃。在本练习中,我们将修复此问题,并添加在程序运行时更改已用笔记本文件的可能性 首先,检查是否有“notebook.txt”文件,并在没有文件的情况下创建一个文件,以此启动程序。如果必须这样

请帮我看看这个问题。我试图提交任务的工具名为viope.com,这是一个自学环境,它在检查代码时有时会出错。我的代码在第一轮似乎运行良好,但我不明白为什么在第二轮选择1时它不打印,它似乎捕获了文件名的更改任务还有另一个持续的项目,笔记本电脑,依赖于用户的操作,如果用户决定读取文件而不向其写入任何内容,它就会崩溃。在本练习中,我们将修复此问题,并添加在程序运行时更改已用笔记本文件的可能性

首先,检查是否有“notebook.txt”文件,并在没有文件的情况下创建一个文件,以此启动程序。如果必须这样做,还应通知用户“未找到默认笔记本,已创建一个”

当此功能起作用时,将第四个选项添加到笔记本中,“(4)更改笔记本”。如果用户选择此选项,系统将提示用户输入新文件“指定新文件的名称:”。如果存在现有文件,则打开该文件并将其加载到笔记本程序中,同时关闭旧的笔记本文件。如果新笔记本文件不存在,系统会通知用户“未检测到具有该名称的笔记本,已创建一个”,并生成一个新文件。另外,在主菜单“Now using file[filename]”中添加一个已用笔记本文件的注释。正确的输出应该如下所示:

>>> 

No default notebook was found, created one.

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 2

Write a new note: Buy milk.

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 4

Give the name of the new file: otherbook.txt

No notebook with that name detected, created one.

Now using file otherbook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 2

Write a new note: Buy pineapples.

Now using file otherbook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 4

Give the name of the new file: notebook.txt

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 1

Buy milk.:::12:05:23 04/25/11



Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 5

Notebook shutting down, thank you.

>>> 
import time
try:
    f=open("notebook.txt","r")

    print("Now using file",f.name)
except Exception:
    print("No default notebook was found, created one.")
    mf=open("notebook.txt","w")
    print("Now using file",mf.name)

mf="notebook.txt"    
promptForNumbers = True
while True:
    if promptForNumbers:
        print("(1) Read the notebook\n(2) Add note\n(3) Empty the notebook\n(4) Change the notebook\n(5) Quit\n")
        selection=int(input("Please select one: "))

    if selection==1:
        handle = open(mf,"r")
        filetext = handle.read()

        print(filetext)
        print("Now using file",handle.name)
    elif selection==2:
        filetext=input("Write a new note: ")

        myfile= open(mf, "w")
        myfile.write(filetext)
        myfile.write(":::")
        myfile.write(time.strftime("%X %x"))
        print("Now using file",myfile.name ) 

    elif selection==3:
        readfile = open(mf,"w")
        readfile.close()
        print("Notes deleted.")
    elif selection == 4:
        mf=input("Give the name of the new file: ")

        try:

            f=open(mf)
            filetext= f.read()
            print("Now using file",f.name)

        except Exception:

            print("No notebook with that name detected, created one.")
            x=open(mf,"w")

            print("Now using file",x.name )
        else:
            f.close
            promptForNumbers = True
    elif selection==5:
        print("Notebook shutting down, thank you.")
        break
    else:
        print("Incorrect selection")
# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy milk.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  otherbook.txt
No notebook with that name detected, created one.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy pineapples.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  notebook.txt
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1
Buy milk.:::21:11:24 11/04/11
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  5
Notebook shutting down, thank you.
# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy pineapples.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  otherbook.txt
No notebook with that name detected, created one.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1

Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  notebook.txt
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1

Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  5
Notebook shutting down, thank you.
我的代码:

>>> 

No default notebook was found, created one.

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 2

Write a new note: Buy milk.

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 4

Give the name of the new file: otherbook.txt

No notebook with that name detected, created one.

Now using file otherbook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 2

Write a new note: Buy pineapples.

Now using file otherbook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 4

Give the name of the new file: notebook.txt

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 1

Buy milk.:::12:05:23 04/25/11



Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 5

Notebook shutting down, thank you.

>>> 
import time
try:
    f=open("notebook.txt","r")

    print("Now using file",f.name)
except Exception:
    print("No default notebook was found, created one.")
    mf=open("notebook.txt","w")
    print("Now using file",mf.name)

mf="notebook.txt"    
promptForNumbers = True
while True:
    if promptForNumbers:
        print("(1) Read the notebook\n(2) Add note\n(3) Empty the notebook\n(4) Change the notebook\n(5) Quit\n")
        selection=int(input("Please select one: "))

    if selection==1:
        handle = open(mf,"r")
        filetext = handle.read()

        print(filetext)
        print("Now using file",handle.name)
    elif selection==2:
        filetext=input("Write a new note: ")

        myfile= open(mf, "w")
        myfile.write(filetext)
        myfile.write(":::")
        myfile.write(time.strftime("%X %x"))
        print("Now using file",myfile.name ) 

    elif selection==3:
        readfile = open(mf,"w")
        readfile.close()
        print("Notes deleted.")
    elif selection == 4:
        mf=input("Give the name of the new file: ")

        try:

            f=open(mf)
            filetext= f.read()
            print("Now using file",f.name)

        except Exception:

            print("No notebook with that name detected, created one.")
            x=open(mf,"w")

            print("Now using file",x.name )
        else:
            f.close
            promptForNumbers = True
    elif selection==5:
        print("Notebook shutting down, thank you.")
        break
    else:
        print("Incorrect selection")
# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy milk.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  otherbook.txt
No notebook with that name detected, created one.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy pineapples.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  notebook.txt
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1
Buy milk.:::21:11:24 11/04/11
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  5
Notebook shutting down, thank you.
# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy pineapples.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  otherbook.txt
No notebook with that name detected, created one.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1

Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  notebook.txt
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1

Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  5
Notebook shutting down, thank you.
我的代码的输出:

>>> 

No default notebook was found, created one.

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 2

Write a new note: Buy milk.

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 4

Give the name of the new file: otherbook.txt

No notebook with that name detected, created one.

Now using file otherbook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 2

Write a new note: Buy pineapples.

Now using file otherbook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 4

Give the name of the new file: notebook.txt

Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 1

Buy milk.:::12:05:23 04/25/11



Now using file notebook.txt

(1) Read the notebook

(2) Add note

(3) Empty the notebook

(4) Change the notebook

(5) Quit



Please select one: 5

Notebook shutting down, thank you.

>>> 
import time
try:
    f=open("notebook.txt","r")

    print("Now using file",f.name)
except Exception:
    print("No default notebook was found, created one.")
    mf=open("notebook.txt","w")
    print("Now using file",mf.name)

mf="notebook.txt"    
promptForNumbers = True
while True:
    if promptForNumbers:
        print("(1) Read the notebook\n(2) Add note\n(3) Empty the notebook\n(4) Change the notebook\n(5) Quit\n")
        selection=int(input("Please select one: "))

    if selection==1:
        handle = open(mf,"r")
        filetext = handle.read()

        print(filetext)
        print("Now using file",handle.name)
    elif selection==2:
        filetext=input("Write a new note: ")

        myfile= open(mf, "w")
        myfile.write(filetext)
        myfile.write(":::")
        myfile.write(time.strftime("%X %x"))
        print("Now using file",myfile.name ) 

    elif selection==3:
        readfile = open(mf,"w")
        readfile.close()
        print("Notes deleted.")
    elif selection == 4:
        mf=input("Give the name of the new file: ")

        try:

            f=open(mf)
            filetext= f.read()
            print("Now using file",f.name)

        except Exception:

            print("No notebook with that name detected, created one.")
            x=open(mf,"w")

            print("Now using file",x.name )
        else:
            f.close
            promptForNumbers = True
    elif selection==5:
        print("Notebook shutting down, thank you.")
        break
    else:
        print("Incorrect selection")
# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy milk.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  otherbook.txt
No notebook with that name detected, created one.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy pineapples.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  notebook.txt
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1
Buy milk.:::21:11:24 11/04/11
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  5
Notebook shutting down, thank you.
# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  2
Write a new note:  Buy pineapples.
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  otherbook.txt
No notebook with that name detected, created one.
Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1

Now using file otherbook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  4
Give the name of the new file:  notebook.txt
Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  1

Now using file notebook.txt
(1) Read the notebook
(2) Add note
(3) Empty the notebook
(4) Change the notebook
(5) Quit

Please select one:  5
Notebook shutting down, thank you.
按照编写代码的方式,如果找不到notebook.txt文件,它会创建一个,因此即使它在那里但没有正确找到,现在也会用一个空白文件过度编写。这是在这里完成的:

try:
    f=open("notebook.txt","r")

    print("Now using file",f.name)
except Exception:
    print("No default notebook was found, created one.")
    mf=open("notebook.txt","w")
    print("Now using file",mf.name)
您是否正在执行某些操作以在程序运行之间更改文件?处理异常的方式会掩盖任何有用的错误消息。我建议将此更改为:

try:
    f=open("notebook.txt","r")

    print("Now using file",f.name)
except IOError as e:
    print("IOErorr: ",e.strerror)
    print("No default notebook was found, created one.")
    mf=open("notebook.txt","w")
    print("Now using file",mf.name)
使用该更改运行它,并查看查找在上一次运行中创建的notebook.txt时出现的问题

还有几件事:

1) 错误输入的错误检查不能处理用户输入非数字的情况


2) 存在一个问题,即变量“mf”可以引用字符串或文件对象,具体取决于执行的位置。也许没问题,但肯定会让人困惑。

嗨,首先感谢您抽出时间来帮助我。我对问题进行了编辑,添加了全部内容,以防您想了解任务要求我完成什么。我理解这个问题的方式是,第一个错误异常处理是一次性的,我想这就是为什么我把它放在while循环之外。