I tried it again with a much bigger archive to make sure it takes a long
while, and still no trouble. What you are saying implies that the unzip
process is somehow async. But your code should be blocking on the entire
thing. Are you doing this to a  network location? If so, you could be
dealing with a network cache or latency. I see this is a windows exception,
so I might not be running into the same issues as you on osx, with how
windows is running the unzips.

What you might need to do then is wrap the last line of your context
manager in a try..except that maybe tries 10 times with some sleeps, before
failing:

@contextlib.contextmanager
def make_temp_directory():
    temp_dir = tempfile.mkdtemp()
    yield temp_dir

    i = 0
    while i < 10:
        try:
            shutil.rmtree(temp_dir)
        except:
            time.sleep(.1)
        else:
            return

    raise IOError("Failed to remove temp path: %s" % temp_dir)



On Mon, Dec 10, 2012 at 1:28 AM, Jamie Telford <[email protected]>wrote:

> Hey Justin..
>
> yeah the manager works.. but tends to go ahead a delete the temp folder
> before all my processes finish..
>
> First step is to unzip a file that contains about 20 more zip files.. then
> I process the temp folder and unzip the zip files from there back into a
> user defined folder.
>
> WindowsError: [Error 32] The process cannot access the file because it is
> being used by another process:
>
> it throws the above error.. and it looks like the the temp folder is being
> deleted before the final file can be unzipped.. i.e. being used by another
> process..
>
> So I'm thinking I need someway of testing if all the files have been
> unzipped first before the tempfile is cleared
>
> cheers for the reply!
>
>
> On Monday, December 10, 2012 12:02:08 AM UTC+8, Justin Israel wrote:
> > What is the exact error you are getting? Your code seems to work fine
> for me.
> > If I understand correctly, you have one main archive that consists of
> multiple archives, and you want to unpack the contents of those packed
> archives into a target location?
> >
> >
> > The only change I made to your code before test, was this, since I am on
> OSX. But it is probably best to do this anyways to be portable:
> >
> >               for files in tempfiles:
> >                       tzf = zipfile.ZipFile(os.path.join(temp_dir,
> files))
> >
> >
> > I compressed some files into two zip files, then compressed those into
> one main one. It ran and extracted the files all into the target directory.
> >
> >
> >
> >
> >
> >
> >
> > On Dec 7, 2012, at 12:48 AM, Jamie Telford wrote:
> > Hi I'm running a script to unzip some files into a temp directory and
> then delete the temp directory once finished..
> > however It seems that the current unzipping process hasn't completed yet
> and throws and error when my context manager tries to remove the temp
> directory..  below is my code..
> >
> >
> >
> >
> >
> > @contextlib.contextmanager
> > def make_temp_directory():
> >     temp_dir = tempfile.mkdtemp()
> >     yield temp_dir
> >     shutil.rmtree(temp_dir)
> >
> >
> >
> > def process(*args):
> >       zipPath = cmds.textField("zipInputField", q = True, text = True)
> >       outLoc = cmds.textField("outputField", q = True, text = True)
> >
> >
> > # unpack files with *.zip extension from zip file to temp directory
> >
> >       with make_temp_directory() as temp_dir:
> >               zf = zipfile.ZipFile(zipPath, 'r')
> >               for fileName in zf.namelist():
> >                       if fileName.endswith(".zip"):
> >                               zf.extract(fileName, temp_dir)
> >
> >
> >
> > # unpack zip files in temp directory to new folder location
> >
> >               tempfiles = os.listdir(temp_dir)
> >               for files in tempfiles:
> >                       tzf = zipfile.ZipFile(temp_dir + "\\" + files)
> >                       tzf.extractall(outLoc)
> >
> >
> > Cheers in advance!
> >
> >
> > Jamie
> >
> >
> >
> > --
> >
> > view archives: http://groups.google.com/group/python_inside_maya
> >
> > change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].


Reply via email to