1

我通过阅读和修改一些原始源文件创建了一些文件。然后我使用 . 将新创建的文件复制到旧文件名shutil。当我尝试删除通过编辑原始源创建的文件时,出现此错误:

WindowsError:[错误 32] 该进程无法访问该文件,因为它正被另一个进程使用:

我试过打开和关闭文件和泡菜

for fileB in newFilesList:
    #print "FileB: %s" % fileB
    if re.search('newfile',fileB,flags=0):
        newFileCounter+=1
        fileToGo=countFiles-newFileCounter
        print("There are %s files left to copy." % fileToGo)
        #print "we have a match!!!!!!!!!!!!!"
        os.chdir(path)
        LocPreNewfile=fileB.find('newfile')
        FileCBase=fileB[0:LocPreNewfile-1]
        FileC= FileCBase + ".shp.iso.xml"
        #print "FileCBase %s" % FileCBase
        #print "FileC: %s " % FileC
        os.chdir(path)
        if os.path.exists(fileB):
            #print "fileB %s exists!!!!!!!!!!!!!!!!!!!!!" % fileB
            if os.path.exists(FileC):
                #print "%s exists!!!!!!!!!!!!!!!!!!!!!" % FileC
                shutil.copyfile(fileB, FileC)
                if countFiles > 1:
                    if fileToGo >0:
                        os.remove(fileB)
                    else:
                        #fileD =fileB
                        print('One file left to remove: %s' % fileB)


            else:
                print ("FileC ( %s ) does not exist" % FileC)
        else:
            print ("%s does not exist" % fileB)
    #print"---------------------------------------------------\n\n"
    #print("---------------------------------------------------\n\n")
    print("---------------------------------------------------\n\n")

if os.path.exists(fileB):
   print("Removing %s" % fileB)

   os.remove(fileB)
4

0 回答 0