1

I'm working with python 2 and have read several posts about this error i.e(this post). However, I'm still getting the error. What I do is: I read the files in a directory, if any of the files contains a specific string, I delete the directory.

def select_poo():
path = os.walk('/paila_candonga/')
texto = 'poo'
extension = '.tex'
for root, dirs, files in path:
    for documento in files:
        if extension in documento:
            with open(os.path.join(root, documento), 'r') as fin:
                for lines in fin:
                    if texto in lines:
                        shutil.rmtree(root)
                    else:
                        continue

Then I get the error:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process

I have also tried using the absolute path:

def select_poo():
path = os.walk('/paila_candonga/')
texto = 'poo'
extension = '.tex'
for root, dirs, files in path:
    for documento in files:
        if extension in documento:
            with open(os.path.join(root, documento), 'r') as fin:
                for lines in fin:
                    if texto in lines:
                        route = (os.path.join(root, documento))
                        files = os.path.basename(route)
                        folder = os.path.dirname(route)
                        absolut= os.path.dirname(os.path.abspath(route))
                        todo = os.path.join(absolut, files)
                        print todo

                    else:
                        continue

Then I will get:

C:\paila_candonga\la_Arepa.tex
C:\paila_candonga\sejodio\laOlla.tex
C:\paila_candonga\sejodio\laPaila.tex

If I remove one file at a time, using the same absolute path and os.remove(''), I won't have problems. If I try to delete all files at once using select_poo() and shutil.rmtree(folder) or os.remove(absolut), I will have the Error 32.

Is there a way I can do a loop through each of the paths in todo and remove them without having the error 32?

Thanks,

Community
  • 1
  • 1
Chüngel
  • 375
  • 4
  • 19

1 Answers1

2

it happens here :

with open(os.path.join(root, documento), 'r') as fin:

So you have your file open and locked, that is why you are not able delete this folder using:

shutil.rmtree(root)

within this statement, you have to do outside of with statement

Alex
  • 1,141
  • 8
  • 13
  • Thanks Alex, but it won't work. The error is still there =( – Chüngel Oct 19 '16 at 13:31
  • @Chüngel are you sure you don't have this folder or any file from this folder opened in any other application ? – Alex Oct 19 '16 at 13:37
  • 1
    Alex, I'm pretty new with Python but If I remove the files using os.remove(absolut) the same files will be removed without errors. So I believe the error is caused by the process followed while I remove all files at the same time. As you suggested, I have defined a variable x = [] then if the string is found in the document, the path will be appended to x. Then Outside the with statement, I execute os.remove(x[0]), but then I get exactly the same error. Can you please suggest an edition to my code based on your idea? – Chüngel Oct 19 '16 at 14:20
  • @Chüngel, Windows uses a sharing mode when opening files. It's a kind of file lock, but while Windows file locking allows mandatory locking of byte ranges in files, the sharing mode instead limits shared read, write, and delete access for the entire file. Every time a file is opened, the requested access and sharing mode have to be compatible with all existing references to the file. Since deleting a file requires opening it with `DELETE` access, all handles for the file that don't share delete access have to be closed beforehand. – Eryk Sun Oct 19 '16 at 19:45
  • @Chüngel, that's not to say that opening files with delete sharing is a way to make Windows behave like a typical POSIX system. There's an additional rule that stands in the way: all open files must always be named, including when a file is memory-mapped as code or data (e.g. loaded EXEs and DLLs). Even when 'deleted', a file remains linked in the parent directory until all handles are closed, and trying to delete a memory-mapped file is always denied. A possible workaround is to first rename the file to a temp directory that's on the same volume. – Eryk Sun Oct 19 '16 at 20:20