python Notebook:更改写入的文件

时间:2014-03-19 10:06:27

标签: python python-3.x

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

首先,通过检查文件是否为“notebook.txt”来启动程序,如果没有,则创建一个。如果必须这样做,还要通知用户警告“未找到默认笔记本,创建一个。”。

当此功能有效时,将第四个选项添加到笔记本中,“(4)更换笔记本”。如果用户选择此选项,则会提示用户输入新文件“提供新文件的名称:”。如果存在现有文件,则会将其打开并加载到笔记本程序中,同时关闭旧笔记本文件。如果新的笔记本文件不存在,系统会通知用户“没有检测到具有该名称的笔记本,创建一个笔记本”。并创建一个新文件。还要在主菜单中添加使用过的笔记本文件的注释,“现在使用文件[文件名]”。正确的输出应如下所示:

>>> 

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.

1 个答案:

答案 0 :(得分:0)

# python3.2 ohjelma.py 
No default notebook was found, created one.
Now using file notebook.txt

编写代码的方式,如果找不到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)

你是否正在做一些事情来改变程序运行之间的文件?处理Exception的方式掩盖了任何有用的错误消息。我建议将其更改为:

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”可以引用一个字符串或一个文件对象,具体取决于你在执行中的位置。也许不是问题,但绝对令人困惑。