0

Trying to make the user write to a file and seeing if it exists and if they should override it

    print("Enter a file name to save your database to.")
    name = input('Enter name of text file: ')+'.txt'
    if name print ("Would you like to overwrite this file? Yes or No")
    if "yes"

Edited by pritaeas: Moved to software dev.

3
Contributors
3
Replies
31
Views
1 Year
Discussion Span
Last Post by Gribouillis
0

You can start with this

import os

print('Enter a file name to save your database to.')
name = input('Enter name of text file: ').strip()
if name:
    name = os.path.expanduser(name + '.txt')
    if os.path.isfile(name):
        while True:
            overwrite = input("Would you like to overwrite this file? Yes or No: ")
            overwrite = overwrite.strip().capitalize()
            if overwrite in ('Yes', 'No'):
                break
            else:
                print('Error: please answer Yes or No')
        if overwrite == 'No':
            raise NotImplementedError("I don't know what to do!")
else:
    raise RuntimeError('Got an empty name')

Edited by Gribouillis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.