Search code examples
python-3.xpermissionspython-3.6shutilpython-os

Python3: File is being used by another process


Normally when submitting code here, I would try to only include the minimum amount of code to demonstrate the problem. However, I did not encounter this problem until the very end of writing the code, which made it almost impossible to debug the problem.

I have a piece of code that converts a file between some different types. That code is functional without any problems. It starts with an XCI file inside of a ZIP file, and converts it to a folder. However, at the very end of the code, I attempted to make my script delete some temporary folders it had created. This causes a somewhat confusing error that I have not been able to successfully troubleshoot.

import os, shutil, zipfile
from shutil import copyfile
from time import sleep

for fileName in os.listdir("D:/start"):
    filePath = "D:/start" + fileName 
    os.makedirs("C:/Users/Julian/Documents/conversion/extract")

    copyfile(filePath, "C:/Users/Julian/Documents/conversion/extract/game.zip")

    print("Extracting C:/Users/Julian/Documents/conversion/extract/game.zip")
    zip_ref = zipfile.ZipFile("C:/Users/Julian/Documents/conversion/extract/game.zip", 'r')
    zip_ref.extractall("C:/Users/Julian/Documents/conversion/extract")
    zip_ref.close()

    for file in os.listdir("C:/Users/Julian/Documents/conversion/extract"):
        if file.endswith(".xci"):
            os.rename(os.path.join("C:/Users/Julian/Documents/conversion/extract", file), "C:/Users/Julian/Documents/conversionh/extract/game.xci")

    os.system("C:/Users/Julian/Documents/conversion/hactool/hactool.exe -t xci C:/Users/Julian/Documents/conversion/extract/game.xci --outdir=C:/Users/Julian/Documents/conversionh/extract")

    fileSizeTupleList = []
    largestSize = 0

    os.chdir("C:/Users/Julian/Documents/conversion/conversion/secure")
    for i in os.listdir(os.curdir):
        if os.path.isfile(i):
            fileSizeTupleList.append((i, os.path.getsize(i)))

    for fileName, fileSize in fileSizeTupleList:
        if fileSize > largestSize:
            largestSize = fileSize
            largestFile = fileName

    print("Extracting C:/Users/Julian/Documents/conversion/extract/secure/" + largestFile)

    os.system("C:/Users/Julian/Documents/conversion/hactool/hactool.exe -t nca -k C:/Users/Julian/Documents/conversion/hactool/keys C:/Users/Julian/Documents/conversion/extract/secure/" + largestFile + " --exefsdir=C:/Users/Julian/Documents/conversion/game --romfs=C:/Users/Julian/Documents/conversion/game/RomFS.romfs")
    shutil.copytree("C:/Users/Julian/Documents/conversion/game", "D:/end/" + os.path.split(filePath)[1][:-4])
    shutil.rmtree("C:/Users/Julian/Documents/conversion/extract")
    shutil.rmtree("C:/Users/Julian/Documents/conversion/game")

This code works fine until it reaches the second to last line. At that point, I reach the following error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/Julian/Documents/conversion/extract\\secure'

I have absolutely no idea what is causing this error, so any help would be appreciated.


Solution

  • You changed your working directory to the directory you're trying to delete with this line:

    os.chdir("C:/Users/Julian/Documents/conversion/conversion/secure")
    

    So you can't delete the directory since your python script process is using it.

    Just change your working directory to the root directory (os.chdir('/')) before you delete "C:/Users/Julian/Documents/conversion/conversion/secure".