What you want is
\unexpanded macro. Although, if you're using it with
\thesection, are you sure this is actually what you want? For example, look at the following:
What this does is write
\thesection to the temporary file and then input it back in later. As you'll see if you run it, it outputs "Something: Section 2"
Is this really the behaviour you want? If you are writing "answers" then presumably, you want it to refer back to the right section where the corresponding question was, in which case you will want it to expand
\thesection at the time of writing.
There may still be stuff you want unexpanded, but think about what you want expanding and what you don't...
There's a lot more information about writing to files in this question: What is the basic mechanism for writing something to an aux file?