0

I'm trying to have a script that will automatically download files and install them in the correct directories. However When i try to delete the downloaded file after everything has been extracted correctly, I get an OS error that its being used by another process. I use with so the file should close correctly, but even if i put manual closes for source and zip_file it still gives this error. Does anyone know why?

It is most definitely being used by the python interpreter and not any other program, i tested this using "Unlocker". So my only conclusion is the file isn't closing after i open it.

# Downloads, updates, and installs the most recent version of flippy. Only tested on Windows operating systems.

import os
import shutil
import zipfile
import urllib
from os.path import expanduser
import maya.cmds as mc
import maya.mel as mel

download_dir = expanduser("~")
scripts_path = mc.internalVar(usd=True)
prefs_path = mc.internalVar(upd=True)

urllib.urlretrieve(r"https://drive.google.com/uc?export=download&id=0BwF-kFX824PuXzZVQmJja3JzNkE", r"%s/flippy.zip" %(download_dir))

with zipfile.ZipFile("%s/flippy.zip" %(download_dir)) as zip_file:
    for member in zip_file.namelist():
        with zip_file.open(member) as source:
            filename = os.path.basename(member)
            if filename == 'flippy.py' or filename == 'flippy_invert_attrs.txt':
                target = file(os.path.join(scripts_path, filename), "wb")
                copy = True
            elif filename == 'flippy_icon.png':
                target = file(os.path.join("%s/icons" %(prefs_path), filename), "wb")
                copy = True
            elif filename == 'shelf_NeoTools.mel':
                target = file(os.path.join("%s/shelves" %(prefs_path), filename), "wb")
                copy = True
            elif filename == 'CHANGELOG.txt':
                copy = False
                for line in source:
                    print line
            else:
                copy = False
            # copy file (taken from zipfile's extract)
            if copy:
                with source, target:
                    shutil.copyfileobj(source, target)
os.remove("%s/flippy.zip" %(download_dir))

This code gives the following error:

# Error: 32
# Traceback (most recent call last):
#   File "<maya console>", line 40, in <module>
# WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'F:/My Documents/flippy.zip' #
Neo Conker
  • 169
  • 1
  • 13
  • Possible duplicates: [Python zipfile 1](http://stackoverflow.com/questions/28350458/python-zipfile-dosent-release-zip-file) & [Python zipfile 2](http://stackoverflow.com/questions/17095309/error-32-python-file-being-used-by-another-process). Have you tried using this: [ZipFile.close()](https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close)? Can you delete the file from your explorer? – DrHaze Mar 25 '15 at 11:38
  • I've tried adding a zip_file.close() to the first indent of the with zipfile.ZipFile statement, and tried adding a source.close() as well, and tried doing a zipfile.close() at the end before the os.remove. Nothing has worked, but i'm using with statements with everything so it should close automatically. I've also tried using try and finally blocks to close the file, and have looked at both of those and many other winodws 32 errors and windows 32 errors pertaining to zip files with no avail. – Neo Conker Mar 25 '15 at 19:50

0 Answers0