Aug-02-2019, 09:27 PM
I'm trying to write some code that will create a directory under an already existing
directory (C:\TIME\SITES). I want to keep asking the user to enter a directory name
if the one they enter already exists. The way it runs now, it prompts for the directory
name twice (the first time it does give the message "Directory already exists". If I enter
the name of a directory that already exists, it prompts me one more time, I enter that same
director name, then the program exits.
How can I get it to keep asking the user to enter a directory name until they enter one that
doesn't already exist?
directory (C:\TIME\SITES). I want to keep asking the user to enter a directory name
if the one they enter already exists. The way it runs now, it prompts for the directory
name twice (the first time it does give the message "Directory already exists". If I enter
the name of a directory that already exists, it prompts me one more time, I enter that same
director name, then the program exits.
How can I get it to keep asking the user to enter a directory name until they enter one that
doesn't already exist?
import os import shutil mydir = input("Enter name of directory: ") os.chdir("C:\\TIM\\SITES\\") dir_exists = (os.path.exists("C:\\TIM\\SITES\\" + mydir)) try: os.mkdir(mydir) except FileExistsError: print("Directory already exists.") mydir = input("Enter name of directory: ")