file.truncate doesn't clear file content

Viewed 21

I'm using python and django for an application, and I have to stock informations in a text file. So I've done this :

def write_in_file(data):
    stock_file = open(os.path.join(sys.path[0], "static/imported_data.txt"), "w")
    stock_file.truncate()
    stock_file.write(data)
    stock_file.close()

It's working like I want and my file contains 'data'. But when I refresh the page, the content of the file is not cleared, and 'data' is rewrited at the end of the file. And when I print something to debug, I see that the function is called every time I refresh the page. The only time my file is properly cleared is when I modify the 'write_in_file' function.

Does anyone know from where this might come from ?

0 Answers
Related