Skip to content

ENH: Stop mangling default figure file name if file exists #10864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/users/next_whats_new/default_filename_suffix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Stop adding a suffix to suggest unique file name
------------------------------------------------

Previously, when saving a figure to a file using the GUI's
save dialog box, if the default filename (based on the
figure window title) already existed on disk, Matplotlib
would append a suffix (e.g. `Figure_1-1.png`), preventing
the dialog from prompting to overwrite the file. This
behaviour has been removed. Now if the file name exists on
disk, the user is prompted whether or not to overwrite it.
This eliminates guesswork, and allows intentional
overwriting, especially when the figure name has been
manually set using `fig.canvas.set_window_title()`.
11 changes: 0 additions & 11 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2275,17 +2275,6 @@ def get_default_filename(self):
default_basename = default_basename.replace(' ', '_')
default_filetype = self.get_default_filetype()
default_filename = default_basename + '.' + default_filetype

save_dir = os.path.expanduser(rcParams['savefig.directory'])

# ensure non-existing filename in save dir
i = 1
while os.path.isfile(os.path.join(save_dir, default_filename)):
# attach numerical count to basename
default_filename = (
'{}-{}.{}'.format(default_basename, i, default_filetype))
i += 1

return default_filename

def switch_backends(self, FigureCanvasClass):
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ def __init__(self,
):
super().__init__(title, parent, action, buttons)
self.set_default_response(Gtk.ResponseType.OK)
self.set_do_overwrite_confirmation(True)

if not path:
path = os.getcwd()
Expand Down
17 changes: 0 additions & 17 deletions lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,3 @@ def test_get_default_filename():
assert filename == 'image.png'
finally:
shutil.rmtree(test_dir)


def test_get_default_filename_already_exists():
# From #3068: Suggest non-existing default filename
try:
test_dir = tempfile.mkdtemp()
plt.rcParams['savefig.directory'] = test_dir
fig = plt.figure()
canvas = FigureCanvasBase(fig)

# create 'image.png' in figure's save dir
open(os.path.join(test_dir, 'image.png'), 'w').close()

filename = canvas.get_default_filename()
assert filename == 'image-1.png'
finally:
shutil.rmtree(test_dir)