kyegupov / py-unrar2

Automatically exported from code.google.com/p/py-unrar2
MIT License
2 stars 4 forks source link

Python 2.6 - Unable to move or delete RAR archive with shutil #4

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Open the Python script I attached, drop the attached archive into it and 
press enter.

What is the expected output? What do you see instead?
I expect the text file to be extracted to the desktop, and the archive to be 
moved to My Documents.

Instead, the file IS extracted but the archive is just copied to My Documents, 
with the original file staying wherever you placed it, and raises a 
WindowsError.

What version of the product are you using? On what operating system?
Windows XP Home SP3

Please provide any additional information below.
Traceback (most recent call last):
  File "C:/Documents and Settings/*******/Desktop/Error.py", line 54, in <module>
    checkExtension()
  File "C:/Documents and Settings/********/Desktop/Error.py", line 18, in checkExtension
    unrar()
  File "C:/Documents and Settings/*********/Desktop/Error.py", line 47, in unrar
    shutil.move(filename , desktop)
  File "C:\Python26\lib\shutil.py", line 265, in move
    os.unlink(src)
WindowsError: [Error 32] The process cannot access the file because it is being 
used by another process: 'Tester.rar'

Original issue reported on code.google.com by WildBama...@gmail.com on 18 Jun 2010 at 2:02

Attachments:

GoogleCodeExporter commented 9 years ago
This is not a bug, maybe just an omission in documentation. To release the hold 
of the file you were reading, you need to close it first. With RarFile objects 
here it is done via deleting. Thus, you should simply add a line "del rar" 
before your "shutil.move" call.

Original comment by yk4e...@gmail.com on 20 Jun 2010 at 9:50

GoogleCodeExporter commented 9 years ago
Adding "del rar" before moving the archive doesn't seem to do anything for me. 
I still get the exact same error when I try to move it.

Original comment by WildBama...@gmail.com on 20 Jun 2010 at 6:31

Attachments:

GoogleCodeExporter commented 9 years ago
Hmm. Will take a look at it.

Original comment by yk4e...@gmail.com on 20 Jun 2010 at 10:01

GoogleCodeExporter commented 9 years ago
Yes, we still have one more reference to the RarFile object, it's in 
"fileInArchive" variable that's still alive after the for loop. I probably 
should convert it to weakref...

Can you try to rewrite the loop like this and see if it helps:

    rar = UnRAR2.RarFile(filename)
    found = False
    print "\n-------------"
    print "Installing..."
    print "-------------\n"

    for fileInArchive in rar.infoiter():
        file = os.path.split(fileInArchive.filename)[-1]                        
        basename, ext = os.path.splitext(file)

        if ext == ".txt":
            print "Found %s. Extracting to My Documents..." % file
            rar.extract("*.txt", desktop)
            found = True

    del fileInArchive
    del rar

Original comment by yk4e...@gmail.com on 22 Jun 2010 at 3:03

GoogleCodeExporter commented 9 years ago
Just tested that, works just fine now.

Original comment by WildBama...@gmail.com on 22 Jun 2010 at 3:27

GoogleCodeExporter commented 9 years ago
Attention: the problem (reference leak) was re-introduced in version 0.99.1.
Unfortunately, even "del" won't help this time.

Will be fixed in 0.99.2.

Original comment by yk4e...@gmail.com on 9 Sep 2010 at 2:49

GoogleCodeExporter commented 9 years ago

Original comment by yk4e...@gmail.com on 9 Sep 2010 at 2:50

GoogleCodeExporter commented 9 years ago
Released 0.99.2 with the fix

Original comment by yk4e...@gmail.com on 9 Sep 2010 at 2:58