Skip to content

Commit 4ac5f11

Browse files
committed
Check if target file already exists
Closes: #239
1 parent 1fd322f commit 4ac5f11

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

bpython/repl.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,16 +707,29 @@ def write2file(self):
707707
if not fn.endswith('.py') and self.config.save_append_py:
708708
fn = fn + '.py'
709709

710+
mode = 'w'
711+
if os.path.exists(fn):
712+
mode = self.interact.file_prompt('%s already exists. Do you want '
713+
'to (c)ancel, (o)verwrite or '
714+
'(a)ppend? ' % (fn, ))
715+
if mode in ('o', 'overwrite'):
716+
mode = 'w'
717+
elif mode in ('a', 'append'):
718+
mode = 'a'
719+
else:
720+
self.interact.notify('Save cancelled.')
721+
return
722+
710723
s = self.formatforfile(self.getstdout())
711724

712725
try:
713-
f = open(fn, 'w')
726+
f = open(fn, mode)
714727
f.write(s)
715728
f.close()
716729
except IOError:
717730
self.interact.notify("Disk write error for file '%s'." % (fn, ))
718731
else:
719-
self.interact.notify('Saved to %s' % (fn, ))
732+
self.interact.notify('Saved to %s.' % (fn, ))
720733

721734
def pastebin(self, s=None):
722735
"""Upload to a pastebin and display the URL in the status bar."""

0 commit comments

Comments
 (0)