Python Forum
Python 3.8 Nested varible not updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.8 Nested varible not updating
#1
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
Reply
#2
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}")
Reply
#3
(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
Reply
#4
(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.
Reply
#5
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
Reply
#6
(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
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 250 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  local varible referenced before assignment SC4R 6 1,465 Jan-10-2023, 10:58 PM
Last Post: snippsat
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  UPDATING PYTHON TO USE LATEST VERSION OF JAVA? nicklesprout 6 7,318 Jul-27-2017, 08:58 PM
Last Post: nicklesprout
  Inputting a varible BlathaBlather 3 3,545 Apr-05-2017, 08:09 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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