So first off I'm a complete newb at Python so sorry if my code is horrible. This is just for a proof of concept, the actual code doesn't really matter, just the function.
So I'm trying to create a directory by specifying a name for it. I want the program to check if a directory with that name already exists and if it does, ask for a new name and create that directory. The checking bit works fine, but when it loops around to ask for a different name it seems to be storing the original one (the duplicate), which then overwrites the new name when it gets returned. This means I keep getting the old directory name returned instead of the new one (hope that makes sense?)
def casefile():
print '\n\nPlease enter a case name/reference: '
case_name = raw_input()
if not os.path.exists('C:\\Users\\laura17\\Documents\\ProgrammeOutput\\{}'.format(case_name)):
os.mkdir('C:\\Users\\laura17\\Documents\\ProgrammeOutput\\{}'.format(case_name))
print '\n{} case file created. All logs will be saved to C:\Users\laura17\Documents\ProgrammeOutput\{}' \
.format(case_name, case_name)
else:
print '\n**Case already exists. Please choose a different case name.**'
casefile()
return case_name
As an example, I printed out case_name just before the return function to see what was happening:
def casefile():
print '\n\nPlease enter a case name/reference: '
case_name = raw_input()
if not os.path.exists('C:\\Users\\laura17\\Documents\\ProgrammeOutput\\{}'.format(case_name)):
os.mkdir('C:\\Users\\laura17\\Documents\\ProgrammeOutput\\{}'.format(case_name))
print '\n{} case file created. All logs will be saved to C:\Users\laura17\Documents\ProgrammeOutput\{}' \
.format(case_name, case_name)
else:
print '\n**Case already exists. Please choose a different case name.**'
casefile()
print case_name
return case_name
Any help would be much appreciated. Thank you.