Python Forum
Variables being overridden to initial values.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables being overridden to initial values.
#1
Hello first post... as a Python user.

A long time PHP coder, trying to make the move over to Django, so I figured I would try my hand at a backup script to get my feet wet with Python. I have played with Python here and there, but this is my first serious attempt at an actual script that I plan to use on a daily bases.

Here my script:
#!/usr/bin/python3*

'''
	Module name:
	Author: 
	Contact: 
	Desc: 
'''
__version__ = '0.0.1'

import shutil
import os, os.path
import datetime



TODAYS_DATE = str(datetime.date.today())

NUMBER_OF_BACKUP = 3

BACKUP_LOCATION = '/home/Superusr/Backup_Scripts/Test_01/Save_Location/'
FOLDER2BACKUP = '/home/Superusr/Backup_Scripts/Test_01/Sample_Folder'

CURRENT_BACKUP_NAME = FOLDER2BACKUP + "_" + TODAYS_DATE

CURRENT_LIST_OF_BACKUPS = []
MOST_RECENT_BACKUP = "Newest"
OLDEST_BACKUP = "Oldest"


def GET_MOST_RECENT_BACKUP_NAME():
	print("Step 2...")
	for names in os.listdir(BACKUP_LOCATION):
		CURRENT_LIST_OF_BACKUPS.append(names)
	
	CURRENT_LIST_OF_BACKUPS.sort()	
	
def SET_MOST_RECENT_BACKUP():
	print("Step 3...")
	MOST_RECENT_BACKUP = CURRENT_LIST_OF_BACKUPS[-1]
	print(MOST_RECENT_BACKUP)
	
def SET_OLDEST_BACKUP():
	print("Step 4...")
	OLDEST_BACKUP = CURRENT_LIST_OF_BACKUPS[0]
	print(OLDEST_BACKUP)



def INITIAL_SETUP():
	print("Step 1...")
	GET_MOST_RECENT_BACKUP_NAME()
	SET_MOST_RECENT_BACKUP()
	SET_OLDEST_BACKUP()

def main():
	INITIAL_SETUP()
	print("Most recent backup is: {}".format(MOST_RECENT_BACKUP))

if __name__ == "__main__": main()
With a result of:
Output:
Step 1... Step 2... Step 3... Sample_Folder_2020-09-15 Step 4... Sample_Folder_2020-09-01 Most recent backup is: Newest ------------------ (program exited with code: 0) Press return to continue
So yes, this is a basic script, I am building it up, but that is the puzzling part. I put the print statements ("print('Step 1...')" in to make sure each function was being called to troubleshoot.
Originally, without the print statements, and the initial variables set to blank ("MOST_RECENT_BACKUP = ''") it was coming back blank with no errors. So after setting an initial value to the variables, and comparing that to the results after each step I realised my variables were being reset to initial setting even though they were defined out of scope. I was thinking it possibly could be because a "global variables" problem vs. a local variable, but the "CURRENT_LIST_OF_BACKUPS" variable is outside, and is initialised and is maintained through the parsing of the script, so I don't know why "MOST_RECENT_BACKUP" and "OLDEST_BACKUP" are not maintaining their new values and are returning to their initial value instead.

I am sure it is a simple oversight on my part, but I honestly don't see it. Wall

Any help would be appreciated, thanks in advance.


OS: Ubuntu 20.04
Python Version: Python 3.8.2
Reply


Messages In This Thread
Variables being overridden to initial values. - by p2bc - Oct-06-2020, 04:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Changing the initial worksheet name in an MS Excel file azizrasul 3 1,013 Oct-02-2022, 07:56 PM
Last Post: azizrasul
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,554 Jul-27-2022, 08:50 PM
Last Post: rob101
  How to move multiple columns to initial position SriRajesh 4 1,469 Jul-02-2022, 10:34 AM
Last Post: deanhystad
  Create array of values from 2 variables paulo79 1 1,128 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,973 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Giving all possible values to four different variables quest_ 7 3,083 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  Print variable values from a list of variables xnightwingx 3 2,680 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Assign dynamic values to Variables srikanthpython 6 3,483 Jun-06-2020, 03:36 PM
Last Post: srikanthpython
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,864 May-29-2020, 04:06 PM
Last Post: Euler2
  Help with initial conditions Dantooine 1 1,385 Oct-18-2019, 07:22 PM
Last Post: UGuntupalli

Forum Jump:

User Panel Messages

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