Yashwanth Nataraj
Yashwanth Nataraj

Reputation: 193

Writing result to an existing file in python

Can i know how i can write obtained result to a File in python. For example, Below code snippet check for state. If it pass it prints pass otherwise fail.

if generalTools.waitAppear('State2.png', '25') == False:
    testData.reportFail("Fail: TestCase Fail")
else:
    testData.reportPass("PASS: TestCase Pass")  

So, here i want to push result to a file instead of printing.

First, it has to check whether the file exists or not .If it exists, it has to clear the content and write to that file. If file doesn't exists. it has to create new file and it has to write. Can any1 give me some idea how i can do that.

Upvotes: 0

Views: 111

Answers (1)

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

You don't have to do anything special. Use the following code:

with open('workfile', 'w') as fout:
    fout.write(result)

Opening a file in 'write' mode creates it if it doesn't exist, or blanks it and starts again from scratch.

Upvotes: 3

Related Questions