简体   繁体   English

python 字符串未写入文件

[英]python string not writing to a file

this function is being called by the way顺便说一下,这个 function 正在被调用

the file is correct 'paragraphs.txt'该文件是正确的 'paragraphs.txt'

but when it runs it finishes with no errors but doesn't write anything to the file但是当它运行时它没有错误地完成但没有向文件写入任何内容

git files file with problem: https://github.com/K-972/english_helper git 文件有问题的文件: https://github.com/K-972/english_helper

write to file test: https://github.com/K-972/file-write-test写入文件测试: 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)

i have wrote a script separately to test writing to a file and it worked so i don't know why this isn't我已经单独编写了一个脚本来测试写入文件并且它有效所以我不知道为什么这不是

If I run your code like this:如果我这样运行你的代码:

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()

And enter '1' through '14' as input, the script writes a paragraphs.txt containing the following:并输入'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.

So, your problem cannot be reproduced.所以,你的问题无法重现。 Also, it's effective the same as this:此外,它的效果与此相同:

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()

And that works just fine as well (appending the text to the file if it exists, creating it otherwise).这也很好用(如果文件存在则将文本附加到文件,否则创建它)。

You're probably not looking for the file in the correct location.您可能没有在正确的位置查找文件。 Try adding this to the start of your script, and see what it says:尝试将其添加到脚本的开头,看看它说了什么:

import os
print(os.getcwd())

That's where you should be looking for the file that was written.那就是您应该寻找已写入文件的地方。

Also, probably unrelated to your actual problem, as pointed out in the comments, to ensure a file is written and properly closed, use a context manager:此外,正如评论中指出的那样,可能与您的实际问题无关,为确保文件已写入并正确关闭,请使用上下文管理器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM