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


Messages In This Thread
Python 3.8 Nested varible not updating - by Teknohead23 - Aug-27-2021, 07:42 PM

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