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

JavaScript

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:

JavaScript

And enter '1' through '14' as input, the script writes a paragraphs.txt containing the following:

JavaScript

So, your problem cannot be reproduced. Also, it’s effective the same as this:

JavaScript

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement