title: PermissionError: [WinError 32]. Process cannot access the file because it is being used by another process
body: <p>I'm writing a function to deal with power flow,but it cannot process well becaus of the PermissionError: [WinError 32]. Body of the function follows:    </p>
  
  <pre><code>ckp = copy.deepcopy(r1)
  bc1 , nc1 , gc1 = copy.deepcopy(b) , copy.deepcopy(n) , copy.deepcopy(g)         #函数内部变量
  #恢复路径上节点参数 
  if os.path.exists(r'e:\results\rspathcheck.py'):
      (open(r'e:\results\rspathcheck.py')).close()
      os.remove(r'e:\results\rspathcheck.py')
  suc = []
  for pt in pathwt:
      bc1 , nc1 , gc1 = copy.deepcopy(b) , copy.deepcopy(n) , copy.deepcopy(g)         #函数内部变量
      nc1[pt[0],1] = 3        #微网作平衡节点
      nc1[pt[-1],1] = 1       #发电机作为PQ节点
      nc1[pt[-1],2] = 1       #发电机启动有功
      nc1[pt[-1],3] = 1       #发电机启动无功
      sv = open(r'e:\results\rspathcheck.py','w')
      sv.write('\n\n   From   {}   To   {}\n\n'.format(pt[0],pt[-1]))
      sv.close()
      for i in pt:
          nc1[i - 1,1] = 1
          nc1[i - 1,2] = 0        #有功指定为0
          nc1[i - 1,3] = 0        #无功指定为0
  #未恢复节点/支路参数
      for j in set(range(1,n.shape[0] + 1)) - set(pt):
          nc1[j - 1,1] = 4
      for i1 in range(b.shape[0]):
          if bc1[i1,0] not in pt or bc1[i1,1] not in pt:
              bc1[i1,10] = 0
      gc1[pt[-1]- 30,7] = gc1[pt[0] - 30,7] = 1       #发电机状态
      for i2 in pt[1:-1]:
          gc1[i2 - 30,7] = 0 
      gc1[0,7] = 1
      ckp['bus'] = nc1
      ckp['branch'] = bc1
      ckp['gen'] = gc1
      rspathcheck = runpf(ckp,slovedcase = r'e:\results\rspathcheck.py')
      #(open(r'e:\results\rspathcheck.py')).close()
      suc.append(rspathcheck[-1]['succsee'])
  (open(r'e:\results\rspathcheck.py')).close()
  </code></pre>
  
  <p><strong><em>And the error is:</em></strong></p>
  
  <pre><code>Traceback (most recent call last):
  
    File "&lt;ipython-input-7-7a0e9e787cd1&gt;", line 3, in &lt;module&gt;
      os.remove(r'e:\results\rspathcheck.py')
  
  PermissionError: [WinError 32]  Process cannot access the file because it is being used by another process.:      'e:\\results\\rspathcheck.py'
  </code></pre>
  
  <p>I would appreciate if anyone could help me fix the problem.</p>