在Python中获取PermissionError:[WinError 32]

时间:2019-01-05 21:56:51

标签: python winapi

我不断得到

  

PermissionError:[WinError 32]进程无法访问文件   因为它正在被另一个进程使用:“ data.txt”,代码,

该如何解决?

 import os
    file=open("data.txt","w")
    y=101
    for x in range (1,101):
        h="Data"+(str(format(x,'03d')))+":"+str(y)+"\n"
        y+=1
        if x==3:
           h="Data"+(str(format(x,'03d')))+":"+str(109)+"\n"
    file.write(h)
    file.close()
    file=open("data.txt","r")
    if int(h[4:6])+100 != int(h[8:11]):

       a=input()
       b=int(input())
       c=int(a[4:7])
       r=101
       file2=open("new.txt","w")
       for d in range (1,101):
           e="Data"+(str(format(d,'03d')))+":"+str(r)+"\n"
           if c+100 != b:
              e="Data"+(str(format(c,'03d')))+":"+str(b)+"\n"
              file2.write(e)
              continue 
          file2.write(e)
          r+=1
      os.remove("data.txt")
      os.rename("new.txt","data.txt")
    file2.close()

1 个答案:

答案 0 :(得分:0)

在第11行(file=open("data.txt","r"))中,您将再次打开data.txt,而不用做任何事情。由于在尝试os.remove("data.txt")时它仍然处于打开状态,因此会收到错误消息。尝试删除第11行,这应该可以解决错误。

有帮助吗?