簡體   English   中英

python 字符串未寫入文件

[英]python string not writing to a file

順便說一下,這個 function 正在被調用

該文件是正確的 'paragraphs.txt'

但是當它運行時它沒有錯誤地完成但沒有向文件寫入任何內容

git 文件有問題的文件: https://github.com/K-972/english_helper

寫入文件測試: https://github.com/K-972/file-write-test


def petal_function():
    type_of_petal = input("""
enter number of what you want
  1: comparing ideas sources a and b
  2: how a character is presented
  3: how a them is presented in a poem""")

    if type_of_petal == '1':
        print('source A\n')
        idea = input('enter idea to explore')
        technique = input('enter technique to explore')
        quote = input('enter quote to explode')
        zoom_in_word = input('enter word to zoom in on')
        wordclass = input('enter word class of word')
        feeling = input('enter feeling created in the readers head')
        why_it_creates_the_feeling = input('why is the feeling created')
        print('\nsource B\n')
        technique_2 = input('enter another technique to explore')
        quote_2 = input('enter another quote to explode')
        zoom_in_word_2 = input('enter word to zoom in on')
        wordclass_2 = input('enter word class of word')
        image_constructed = input('enter image constructed in the readers head')
        why_it_creates_the_feeling_2 = input('enter why is the feeling created')


        petal = (f"""In source A the idea of {idea} is presented through the use of {technique} this is evident in the quote \'{quote}\'.
the {wordclass} \'{zoom_in_word}\' create\'s a {feeling} feeling in the reader\'s head and presents the idea of {idea} because
{why_it_creates_the_feeling}. However in source B the idea of {idea} is presented through the use of {technique_2} this is
shown in the quote \'{quote_2}\'. the {wordclass_2} \'{zoom_in_word_2}\' create\'s a {image_constructed} image in the reader\'s 
head and presents the idea of {idea} because {why_it_creates_the_feeling_2}.\n""")

        file = open('paragraphs.txt', 'a')
        file.write(petal)

我已經單獨編寫了一個腳本來測試寫入文件並且它有效所以我不知道為什么這不是

如果我這樣運行你的代碼:

def petal_function():
    type_of_petal = input("""
enter number of what you want
  1: comparing ideas sources a and b
  2: how a character is presented
  3: how a them is presented in a poem""")

    if type_of_petal == '1':
        print('source A\n')
        idea = input('enter idea to explore')
        technique = input('enter technique to explore')
        quote = input('enter quote to explode')
        zoom_in_word = input('enter word to zoom in on')
        wordclass = input('enter word class of word')
        feeling = input('enter feeling created in the readers head')
        why_it_creates_the_feeling = input('why is the feeling created')
        print('\nsource B\n')
        technique_2 = input('enter another technique to explore')
        quote_2 = input('enter another quote to explode')
        zoom_in_word_2 = input('enter word to zoom in on')
        wordclass_2 = input('enter word class of word')
        image_constructed = input('enter image constructed in the readers head')
        why_it_creates_the_feeling_2 = input('enter why is the feeling created')


        petal = (f"""In source A the idea of {idea} is presented through the use of {technique} this is evident in the quote \'{quote}\'.
the {wordclass} \'{zoom_in_word}\' create\'s a {feeling} feeling in the reader\'s head and presents the idea of {idea} because
{why_it_creates_the_feeling}. However in source B the idea of {idea} is presented through the use of {technique_2} this is
shown in the quote \'{quote_2}\'. the {wordclass_2} \'{zoom_in_word_2}\' create\'s a {image_constructed} image in the reader\'s 
head and presents the idea of {idea} because {why_it_creates_the_feeling_2}.\n""")

        file = open('paragraphs.txt', 'a')
        file.write(petal)


petal_function()

並輸入'1''14'作為輸入,腳本會寫入包含以下內容的paragraphs.txt

In source A the idea of 2 is presented through the use of 3 this is evident in the quote '4'.
the 6 '5' create's a 7 feeling in the reader's head and presents the idea of 2 because
8. However in source B the idea of 2 is presented through the use of 9 this is
shown in the quote '10'. the 12 '11' create's a 13 image in the reader's 
head and presents the idea of 2 because 14.

所以,你的問題無法重現。 此外,它的效果與此相同:

def petal_function():
    type_of_petal = input('Enter a 1 or something else: ')

    if type_of_petal == '1':
        petal = 'Hello world'

        file = open('paragraphs.txt', 'a')
        file.write(petal)


petal_function()

這也很好用(如果文件存在則將文本附加到文件,否則創建它)。

您可能沒有在正確的位置查找文件。 嘗試將其添加到腳本的開頭,看看它說了什么:

import os
print(os.getcwd())

那就是您應該尋找已寫入文件的地方。

此外,正如評論中指出的那樣,可能與您的實際問題無關,為確保文件已寫入並正確關閉,請使用上下文管理器:

with open('paragraphs.txt') as f:
    f.write(petal)

暫無
暫無

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

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