Aug-27-2021, 08:01 PM
Quote:I assumed if the nested variable updates the other would automatically.
No, that's not how python str()s work. They are just a sequence of characters. It's not a dynamic formula that updates. I wouldn't even call this a "nested" variable. This is just a static string that is created from the values of other strings. If the other strings change in the future, it doesn't affect the one you built.
name = "Sally" mystring = "Hello, " + name name = "Miguel" # Does not affect the mystring value print(mystring) # prints "Hello Sally"
Something like this might work. I'm not sure what logic you were using for determining the number of digits that could be used. This one just hardcodes it to exactly 3. But making that dynamic on some input or other signal would not be difficult.
You can't create a directory with a conflicting name. As long as you're talking a small number of directories (1000 is still small), then it makes sense to just try to create the next one and handle the failure if it exists.
import os from pathlib import Path drawing_dir = Path("C:", "Users", "user", "Documents", "Client Drawings") letters = input("Enter the first 3 letters: ") letters = letters.upper() for digits in range(1000): dir_name = drawing_dir / f"{letters}{digits:03}" try: os.mkdir(dir_name) print (f"Created {dir_name} as next free directory") break except FileExistsError: continue else: print(f"All three-digit numbers taken for {letters}")