Oct-06-2020, 04:43 PM
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:
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.
Any help would be appreciated, thanks in advance.
OS: Ubuntu 20.04
Python Version: Python 3.8.2
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.
Any help would be appreciated, thanks in advance.
OS: Ubuntu 20.04
Python Version: Python 3.8.2