PermissionError:[WinError 32]。进程无法访问该文件,因为它正由另一个进程使用

时间:2016-10-07 06:41:51

标签: python python-3.x

我正在编写一个处理功率流的函数,但由于PermissionError:[WinError 32],它无法很好地处理。函数体如下:

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()

错误是:

Traceback (most recent call last):

  File "<ipython-input-7-7a0e9e787cd1>", line 3, in <module>
    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'

如果有人能帮助我解决问题,我将不胜感激。

0 个答案:

没有答案