Aug-27-2021, 07:42 PM
I am trying to look for a folder & create it if it does not exist, if it does look for the next number up, Win 10 Pycharm 3.8
Creating the folder works fine if it does not exist but having issues with the last else statement when it does.
I can't understand why the num_start variable updates but the dir_look variable does not.
I've been told that it's because I have updated one variable but not the other & I don't get that logic as I assumed if the nested variable updates the other would automatically.
If I use a while loop, which I think I should, I get an infinite loop as it will always be true
PS, starting coding after a 40-year absence, it had rubber keys & a separate tape deck
Creating the folder works fine if it does not exist but having issues with the last else statement when it does.
I can't understand why the num_start variable updates but the dir_look variable does not.
I've been told that it's because I have updated one variable but not the other & I don't get that logic as I assumed if the nested variable updates the other would automatically.
If I use a while loop, which I think I should, I get an infinite loop as it will always be true
PS, starting coding after a 40-year absence, it had rubber keys & a separate tape deck
import os letters = input("Enter the first 3 letters : ") letters = letters.upper() num_start = 1 zero = "00" num_start_len = len(str(num_start)) print("num start length is ", num_start_len) dir_look = (r"C:\Users\user\Documents\Client Drawings\\" + letters + zero + str(num_start)) check_for_folder = os.path.isdir(dir_look) print(check_for_folder) # working out how many zero digits required if num_start_len == 3 : zero = "" print("If" + zero) elif num_start_len == 2 : zero = "0" print("elif" + zero) else: print("else" + zero) # make directory if not check_for_folder : os.makedirs(dir_look) print("created folder : ", dir_look) else: if check_for_folder: num_start += 1 # os.makedirs(dir_look) print("Num start value", num_start) print(dir_look) else: print("end") print(dir_look)
Output:Enter the first 3 letters : aaa
num start length is 1
True
else00
Num start value 2
C:\Users\user\Documents\Client Drawings\\AAA001
C:\Users\user\Documents\Client Drawings\\AAA001
Process finished with exit code 0