Cannot Remove File: Device or resource busy

Brett Bowman bnbowman at gmail.com
Tue Nov 16 20:37:10 EST 2010


I'm spawning a subprocess to fix some formating errors with a library of
PDFs with pdftk:
    try:
        sp = subprocess.Popen('pdftk.exe "%s" output %s' % (pdfFile,
outputFile))
        sp.wait()
        del sp
    except Exception, e:
        return "Unable to open file: %s with error: %s" % (pdfFile, str(e))

And then I test the result:
    try:
        pdf_handle = open(outputFile, "rb")
        pdf_pypdf = PdfFileReader(pdf_handle)
        del pdf_pypdf
        del pdf_handle
    except Exception, e:
        return "Unable to open file: %s with error: %s" % (outputFile,
str(e))

Both of which appear to work.  But when I try to delete the original
pdfFile, I get an error message saying that the file is still in use.

if I use:
    sp = subprocess.Popen('rm "%s"' % pdfFile)
    sp.wait()
I get the message - the standard error message from rm

and if I use:
    cwd = os.getcwd()
    os.remove(cwd + "\\" + pdfFile)
I get "WindowsError: [Error 32]"  saying much the same thing.

What am I missing?  Any suggestions would be appreciated.

Details:
Python 2.6
Windows XP

Sincerely,
Brett Bowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101116/c1c2f3a5/attachment.html>


More information about the Python-list mailing list