file is not defined, python 3.5
<p>I try to create an xml file. THis is the program 
 I am using python 3.5 and as IDE pycharm. Do I miss a library? Or something?
Or is it the IDE what not correct is?</p>

<p>but I get this error:</p>

<pre><code>NameError: name 'file' is not defined
</code></pre>

<p>I have this:</p>

<pre><code>import datetime
import random
import time

def main():

 # Write an XML file with the results
   file = open("ListAccessTiming.xml","w")

   file.write('&lt;?xml version="1.0" encoding="UTF-8" standalone="no" ?&gt;\n')

file.write('&lt;Plot title="Average List Element Access Time"&gt;\n')


xmin = 1000
xmax = 200000

  # Record the list sizes in xList and the average access time within
 # a list that size in yList for 1000 retrievals.
xList = []
yList = []

for x in range(xmin, xmax+1, 1000):

 xList.append(x)

 prod = 0




 lst = [0] * x

 # let any garbage collection/memory allocation complete or at least
 # settle down
 time.sleep(1)

 # Time before the 1000 test retrievals
 starttime = datetime.datetime.now()

 for v in range(1000):
 # Find a random location within the list
 # and retrieve a value. Do a dummy operation
 # with that value to ensure it is really retrieved.
  index = random.randint(0,x-1)
 val = lst[index]
 prod = prod * val
 # Time after the 1000 test retrievals
 endtime = datetime.datetime.now()

 # The difference in time between start and end.
 deltaT = endtime - starttime

 # Divide by 1000 for the average access time
 # But also multiply by 1000000 for microseconds.
 accessTime = deltaT.total_seconds() * 1000

 yList.append(accessTime)

 file.write(' &lt;Axes&gt;\n')
 file.write(' &lt;XAxis min="â+str(xmin)+â" max="â+str(xmax)+â"&gt;List Size&lt;/XAxis&gt;\n')
 file.write(' &lt;YAxis min="â+str(min(yList))+â" max="â+str(60)+â"&gt;Microseconds&lt;/YAxis&gt;\n')
 file.write(' &lt;/Axes&gt;\n')
 file.write(' &lt;Sequence title="Average Access Time vs List Size" color="red"&gt;\n')

 for i in range(len(xList)):
  file.write('&lt;DataPoint x="â+str(xList[i])+â" y="â+str(yList[i])+â"/&gt;\n')

 file.write('&lt;/Sequence&gt;\n')

 # This part of the program tests access at 100 random locations within a list
 # of 200,000 elements to see that all the locations can be accessed in
 # about the same amount of time.
 xList = lst
 yList = [0] * 200000

 time.sleep(2)

 for i in range(100):
  starttime = datetime.datetime.now()
 index = random.randint(0,200000-1)
 xList[index] = xList[index] + 1
 endtime = datetime.datetime.now()
 deltaT = endtime - starttime
 yList[index] = yList[index] + deltaT.total_seconds() * 1000000

 file.write('&lt;Sequence title="Access Time Distribution" color="blue"&gt;\n');

 for i in range(len(xList)):
  if xList[i] &gt; 0:
   file.write('&lt;DataPoint x="â+str(i)+â" y="â+str(yList[i]/xList[i])+â"/&gt;\n')
file.write('&lt;/Sequence&gt;\n')
file.write('&lt;/Plot&gt;\n')
file.close()
if __name__ == "__main__":
    main()
</code></pre>

<p>Thank you</p>

<p>tab problem !</p>

<p>file variable is not into def main section </p>

<p>def main and file have not indentation </p>

<p>also your main is not called in your tool's</p>

python python-3.x