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?

Community
  • 1
  • 1
piton_hunter
  • 89
  • 1
  • 6
  • 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. – Hara Apr 12 '17 at 11:52
  • I run command line as administrator and have same error. =( – piton_hunter Apr 12 '17 at 11:58

1 Answers1

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.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • 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. – Eryk Sun Apr 12 '17 at 15:15
  • @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. – ivan_pozdeev Apr 12 '17 at 15:27
  • 1
    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. – Eryk Sun Apr 12 '17 at 15:59