BUG: do not remove file if passed in file object

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