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"

Recommended Answers

All 3 Replies

What language are you using? What code have you tried?

I'm using Python

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')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.