Skip to content

Commit 99af2f4

Browse files
committed
RF/ENH: remove file prior writing in to_filename
Closes #465
1 parent 82a261c commit 99af2f4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

nibabel/filebasedimages.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
''' Common interface for any image format--volume or surface, binary or xml.'''
1010

11+
import os
1112
import warnings
1213

1314
from .externals.six import string_types
@@ -347,7 +348,15 @@ def to_filename(self, filename):
347348
-------
348349
None
349350
'''
350-
self.file_map = self.filespec_to_file_map(filename)
351+
self.file_map = file_map = self.filespec_to_file_map(filename)
352+
for _, fh in file_map.items():
353+
if not isinstance(fh, FileHolder):
354+
continue
355+
if os.path.exists(fh.filename):
356+
# Remove previous file where new file would be saved
357+
# Necessary e.g. for cases where file is a symlink pointing
358+
# to some non-writable file (e.g. under git annex control)
359+
os.unlink(fh.filename)
351360
self.to_file_map()
352361

353362
def to_filespec(self, filename):

0 commit comments

Comments
 (0)