If myVariable is a string that comes from an external source (like a database), you first need to find out what kind of string it is.
Since you seem to be using python2, there are two main possibilities: myVariable is either a unicode string object, or a bytes string object. A unicode string is one that has already been decoded to text characters. A bytes string is one that has already been encoded (using an encoding like 'utf-8' or 'latin-1').
It appears from the example code in your question that myVariable is a bytes string object.
The reason you get the first UnicodeDecodeError is because you are trying to re-encode a byte string. To do this, python would first have to decode myVariable to a unicode string object before it could apply the new encoding. By default, python assumes an "ascii" encoding when automatically decoding in this way - but since myVariable contains bytes beyond the ascii range (0-128), an error occurs.
The same situation occurs when you try to pass myVariable to the unicode function. Unless an explicit encoding is given, python will again assume "ascii", and you will see the same UnicodeDecodeError.
Now, when it comes to writing myVariable to a file, the solution is very simple if it is a bytes string object: do nothing! Just write myVariable directly to the file:
f = open(path, 'wb')
f.write(myVariable)
f.close()
However, when you read the file back, you will need to know the original encoding of myVariable in order to decode it to unicode:
f = open(path)
myVariable = f.read().decode('utf-8')
f.close()
And now if you modify myVariable and want to write it back out to file again, you have to remember that this time it is a unicode string, and so you need to encode it first:
f = open(path, 'wb')
f.write(myVariable.encode('utf-8'))
f.close()