2

I'm trying to delete folder using second answer from this question. My folder contains some subfolders, which contain Thumbs.db. So I get error:

PermissionError: [WinError 32] Процесс не может получить доступ к файлу, так как этот файл занят другим процессом: 'C:/foto/my_foto/Thumbs.db'

when I try to delete C:/foto/my_foto/Thumbs.db.

How can I delete this file?

2
  • if you run python script in command line, open command prompt "run as administrator", if python is running from any ide, run your ide as administrator and run your script. Commented Apr 12, 2017 at 11:52
  • I run command line as administrator and have same error. =( Commented Apr 12, 2017 at 11:58

1 Answer 1

2

The same as in other languages - c#, c++, java - because it's specific to the OS and not the language.

There are a few options, only the last one is devoid of nasty side effects:

In brief, the right way is to make the program that's getting in your way stop doing that. Or, if that program has a legitimate reason to keep locked files there, rethink your directory usage patterns.

Sign up to request clarification or add additional context in comments.

3 Comments

Option 2 has too many pitfalls. Even if it works for the immediate problem, it could lead to data corruption if the program opens another file that reuses the closed handle value. It would be better to just kill the program -- gracefully if possible.
@eryksun killing a random unrelated process stops it from doing its job entirely while closing a handle only (presumably) kills the one undesired activity. If that's a process like explorer, killing is not an option at all. Closing handles is a widely suggested way (there are programs like Unlocker specifically written for this), so I had to mention it - as well as its drawbacks.
I upvoted for options 1 and 3, and I mentioned killing as a better (not good) solution than trying to close handles. Programs can be hardened to deal with being killed. They may even have a graceful shutdown via WM_CLOSE or WM_QUIT. Even a console app can be shutdown gracefully if it has a control handler, but only if it's the only process that's attached to the console. However, sneaking in to close a handle isn't something a program should reasonably expect, and it can lead to data corruption when the handle value is reused, which is completely the fault of whoever closed the handle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.