1

I'm trying to delete an empty directory that is open in command line. If the directory is empty, is there still a process connected to it? I've been messing with the psutil package, but I haven't been able to find a process connected to the open directory (ie. I can find the name() and cmdline() but I cannot find the open file). I'm trying to accomplish this in python and it has to be automated. I know there are a lot of posts on this topic, but I can't find anything specifically to find the process of an open empty directory or any other way to remove it.

To clarify, I'm using rmtree(filename) and getting PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: filename

EDIT: This only happens when the directory is open in command line. The directory can be removed if opened in windows explorer. I can see that command line is holding the process, but how do I determine which command line process is associated with the open directory in order to kill that process but not others?

  • If Python's current working directory is the same then you won't be able to remove it. `os.chdir('..')` to move up a directory. – Peter Wood Jun 30 '17 at 21:29
  • @PeterWood `os.chdir(os.pardir)` is the cross-platform solution – Artyer Jun 30 '17 at 21:35
  • It's probably another process holding it, in that case you'll need to import `pywin32`, list all processes holding open files, find the pid and kill that pid – Torxed Jun 30 '17 at 21:41
  • @Artyer I was just addressing [Windows and POSIX](https://docs.python.org/2/library/os.html#os.pardir). Other platforms will have to work it out for themselves. What platform did you have in mind? – Peter Wood Jun 30 '17 at 21:42

0 Answers0