簡體   English   中英

為什么它運行我這個錯誤:AttributeError:'str' object 沒有屬性'write'?

[英]why is it running me this Error: AttributeError: 'str' object has no attribute 'write'?

# Global variable for flash cards
flash_cards = {}


def read_flash_cards_from_text_file():
    """Will read line by line the card name and definition from a local txt file"""
    global flash_cards

    with open("/Users/falks/pyproject1/my_env/Quizlet/flash_cards.txt", "r") as f:
     for lines in f:
         lines_stripped = lines.strip()
         index_split = lines_stripped.index("=")
         key = lines_stripped[0:index_split].strip()
         values = lines_stripped[index_split+1:].strip()
         flash_cards[key] = values
    print(flash_cards)



    if len(flash_cards) < 4:
         print("You must have at least 4 flash cards in text file before starting Quizlet, exiting program now")
         sys.exit()



#read_flash_cards_from_text_file()
def update_flash_cards(key, values, save_to_file=True, delete_flash_card=False):
    """Will be called whenever adding or deleting or replacing a flash card"""
    global flash_cards
    if delete_flash_card:
        del flash_cards[key]

    if save_to_file:
        write_flash_cards_to_text_file()



def write_flash_cards_to_text_file():
    """Will line by line write the name and definition for each flash card to a file for saving"""
    global flash_cards

    with open("./flash_cards.txt", "w+") as f:
        for l, f in flash_cards.items():
            f.write(l + "=" + f + "\n")



def add_flash_card():
    """Will be called from main menu to create or update flash card"""
    global flash_cards

    save_file = False
    while save_file is False:
     key = input("Write down your Name of the flash card: " )
     values = input("write down your defintion of the card name: ")
     save_to_file = input("are you sure y/n: ")
     if save_to_file == "n":
        continue
     if save_to_file == "y":
         save_to_file = True
         save_file = True
     return update_flash_cards(key, values, save_to_file)

read_flash_cards_from_text_file()

add_flash_card()

有人可以解釋一下,為什么它運行我這個錯誤:

{'g':'5','f':'4','r':'6','t':'12'}

寫下您的 flash 卡的名稱:w

寫下您對卡名的定義:r

你確定是/否:是

回溯(最后一次調用):文件“C:\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py”,第 88 行,在 add_flash_card() 文件“C:\Users\falks\pyproject1\my_env\Quizlet\ QuitzletTry.py”,第 77 行,在 add_flash_card 返回 update_flash_cards(key, values, save_to_file) 文件“C:\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py”,第 49 行,在 update_flash_cards write_flash_cards_to_text_file() 文件“C :\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py",第 59 行,write_flash_cards_to_text_file f.write(l + "=" + f + "\n") AttributeError: 'str' object has no attribute 'write '

沒看懂,謝謝幫忙它確實刪除了 flash_cards,txt。 但不會覆蓋它。

如果有人想知道,我編寫了一個簡單的 Quizlet 程序來鍛煉。

問題就在這里。 您對文件 object 和循環變量使用相同的名稱。 當您調用f.write(l + "=" + f + "\n")時, f不再是文件對象(因為循環變量將f覆蓋為字符串對象)

with open("./flash_cards.txt", "w+") as f:
    for l, f in flash_cards.items():
       f.write(l + "=" + f + "\n")

你有f在你的for里面with

您可以選擇另一個變量,問題將被消除。 例如:

    with open("./flash_cards.txt", "w+") as f:
        for l, value in flash_cards.items():
            f.write(l + "=" + value + "\n")

暫無
暫無

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

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