Skip to content
Advertisement

python string not writing to a file

this function is being called by the way

the file is correct ‘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

write to file 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 An')
        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 Bn')
        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

Advertisement

Answer

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 An')
        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 Bn')
        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:

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)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement