This question is not tied directly to python, but I need working implementation under python32 under windows.
Starting from this answer I assume that using shutil.rmtree()
is really slow (I need to delete more than 3M files a day and it takes more than 24 hours) under windows so I wanted to use subprocess.call()
and rmdir
, but since I have cygwin in my %PATH%
system variable wrong rmdir
gets called and I'll get this:
>>> args = ['rmdir', r'D:\tmp']
>>> subprocess.call(args)
cygwin warning:
MS-DOS style path detected: D:\tmp
Preferred POSIX equivalent is: /cygdrive/d/tmp
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
rmdir: failed to remove `D:\\tmp': Directory not empty
1
Note: I know it's required to use /S /Q
to delete folders recursively.
How can I ensure that the right rmdir
is called (like under linux you would use absolute path - /bin/rm
) preferably without using shell=True
?
Is there alternative utility for that (something like using robocopy /MIR
)?
Edit: speed comparison
I've tested different methods of deleting 237 GB (255,007,568,228 bytes) in 1,257,449 Files, 750,251 Folders using Measure-Command
.
+-------------------+-------------+----------+-----------------+
| | rmdir /s /q | shutil | SHFileOperation |
+-------------------+-------------+----------+-----------------+
| Hours | 3 | 5 | 6 |
| Minutes | 26 | 52 | 14 |
| Seconds | 46 | 13 | 48 |
| TotalMinutes | 207 | 352 | 375 |
| TotalSeconds | 12406 | 21134 | 22488 |
| TotalMilliseconds | 12406040 | 21133805 | 22488436 |
+-------------------+-------------+----------+-----------------+
Note: test was run on production server (so results may be affected)