Is it possible to define a variable as open("file.txt", "a") and call it more than once so you don't have to keep typing open("file.txt", "a")?
I tried, but it doesn't seem to work for me. I keep getting the error message:
ValueError: I/O operation on closed file.
My code looks like:
x = open("test.txt", "a")
with x as xfile:
xfile.write("hi")
#works fine until I try again later in the script
with x as yfile:
yfile.write("hello")
Question: Is there a way to do this that I'm missing?
(My apologies if this question is a repeat, I did search google and SO before I posted a new question.)