Python Forum
Python 3.8 Nested varible not updating - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python 3.8 Nested varible not updating (/thread-34744.html)



Python 3.8 Nested varible not updating - Teknohead23 - Aug-27-2021

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

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



RE: Python 3.8 Nested varible not updating - bowlofred - Aug-27-2021

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}")



RE: Python 3.8 Nested varible not updating - snippsat - Aug-27-2021

(Aug-27-2021, 07:42 PM)Teknohead23 Wrote: If I use a while loop, which I think I should, I get an infinite loop as it will always be true
A for loop is always the first choice in Python,here a example with enumerate and f-string which you should use.
import os

letters = input("Enter the first 3 letters : ").upper()
num_start = 1
for index,item in enumerate(letters, num_start):
    dir_look = fr"C:\Users\user\Documents\Client Drawings\{letters}{index:03}"
    print(dir_look)
    check_for_folder = os.path.isdir(dir_look)
    print(check_for_folder
Output:
Enter the first 3 letters : aaa C:\Users\user\Documents\Client Drawings\AAA001 False C:\Users\user\Documents\Client Drawings\AAA002 False C:\Users\user\Documents\Client Drawings\AAA003 False



RE: Python 3.8 Nested varible not updating - bowlofred - Aug-27-2021

(Aug-27-2021, 08:34 PM)snippsat Wrote:
for index,item in enumerate(letters, num_start):

I don't understand that loop. item is never used and the number of characters in letters doesn't seem relevant.


RE: Python 3.8 Nested varible not updating - Teknohead23 - Sep-09-2021

Hello, apologies for the late reply, RTX failure
should have added more description initially, first 3 letters are the client's surname + 001 etc = drawing number & its folder, i am unlikely to hit the ???999 ceiling but understand the thought process of expandability, for its purpose if I did hit the limit I would just add a couple of digits.

Thank you very much for your replies, i shall play with these over the weekend


RE: Python 3.8 Nested varible not updating - Teknohead23 - Oct-02-2021

(Aug-27-2021, 08:34 PM)snippsat Wrote:
(Aug-27-2021, 07:42 PM)Teknohead23 Wrote: If I use a while loop, which I think I should, I get an infinite loop as it will always be true
A for loop is always the first choice in Python,here a example with enumerate and f-string which you should use.
import os

letters = input("Enter the first 3 letters : ").upper()
num_start = 1
for index,item in enumerate(letters, num_start):
    dir_look = fr"C:\Users\user\Documents\Client Drawings\{letters}{index:03}"
    print(dir_look)
    check_for_folder = os.path.isdir(dir_look)
    print(check_for_folder
Output:
Enter the first 3 letters : aaa C:\Users\user\Documents\Client Drawings\AAA001 False C:\Users\user\Documents\Client Drawings\AAA002 False C:\Users\user\Documents\Client Drawings\AAA003 False

I cant use enumerate as it's only going to iterate 3 times as there are 3 input letters, I tried using aaaa, and it iterates 4 times, but you get AAAA001
So for the programs purpose, it's not a viable solution


RE: Python 3.8 Nested varible not updating - Teknohead23 - Oct-02-2021

(Aug-27-2021, 08:01 PM)bowlofred Wrote:
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}")

Hello, This works with some slight amendments
I cut down the range & started at 1
I kept getting an error on the file path so after trial & error added \\ after C:

Thanks for your input