Python Forum
Help repeately prompting for directory name if it already exists.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help repeately prompting for directory name if it already exists.
#3
I took a look at that example but it wasn't making sense to me, so I found another snippet. I almost have it, but when I run the code below, it's giving me a syntax error and I don't know why:

import os
import shutil

newdir = input("Enter name of directory: ")
dir_exists = (os.path.exists("C:\\TIM\\SITES\\" + newdir))
newdir = ("C:\\TIM\\SITES\\" + newdir)
support_files = ("C:\\TIM\\SITES\\FILES FOR CONFIG BUILD\\")

while dir_exists:
	try:
		newdir = input("Enter name of directory: ")
		dir_exists = (os.path.exists("C:\\TIM\\SITES\\" + newdir))
	else:
		os.mkdir(newdir)
Here's what I see when I run it. I can't figure out what it doesn't like about the else statement:
C:\Users\tomel\Desktop>testing.py
File "C:\Users\tomel\Desktop\testing.py", line 14
else:
^
SyntaxError: invalid syntax
Reply


Messages In This Thread
RE: Help repeately prompting for directory name if it already exists. - by hikerguy62 - Aug-05-2019, 01:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,203 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  create a new directory if not already exists fioranosnake 1 1,738 Aug-28-2019, 07:26 PM
Last Post: Larz60+
  Prompting user for number, reading number, squaring it and returning result JHPythonLearner 5 3,111 Sep-13-2018, 04:05 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020