簡體   English   中英

python Notebook:更改寫入的文件

[英]python Notebook: Changing the written file

請幫我看看這個問題。 我嘗試提交任務的工具稱為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.
# 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)

您是否正在做一些更改程序運行之間的文件? 您處理異常的方式會掩蓋所有有用的錯誤消息。 我建議將其更改為:

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”可以引用字符串或文件對象,具體取決於您在執行中的位置。 也許不是問題,但絕對令人困惑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM