Evan
Evan

Reputation: 31

The .write command won't work on python

The .write function isn't working please help. I have gotten it to work before and it works once in my program but only with one document.

import os
file= open("path_to_file", 'r+')
text = input()

the next line of open has the variable as part of the path and is creating a new txt doc

test= open("bla/bla/bla/" + text + ".txt", 'a')
file.write("bla bla bla")
test.write("bla bla bla")

The txt documents stored as "file" when i open it the new text is there but with the text document stored as "test" when I open it there is no text. please help thank you.

Upvotes: 0

Views: 1260

Answers (2)

Evan
Evan

Reputation: 31

Thanks for all your help guys it turned out all i needed to do was:

test.flush()

Upvotes: 3

Gauthier Boaglio
Gauthier Boaglio

Reputation: 10272

You probably need to close after writing :

test= open("bla/bla/bla/" + text + ".txt", 'a')
test.write("bla bla bla")
test.close()

This is always a good practice, if you don't the result can be quite randomized...

Upvotes: 0

Related Questions