Dec-01-2017, 07:40 PM
(Dec-01-2017, 06:01 PM)snippsat Wrote:(Dec-01-2017, 02:59 PM)QueenSvetlana Wrote: The way I've declared file_name and files_to_backup are not in a global way, correct?That's not correct the are in Global namespace,
so the are global variables in that code,
but they are fine as the are the last step and can not interfere with other stuff.
Suppose I rewrote my script to look like this:
def read_data_from_file(file_to_read): #code to read from file and build list return list_of_files def copy_files_to_backup_folder(file_to_copy): #code to copy files def is_backup_successful(): #code to verify that the a compressed file was created def write_log_file(file_name): #code to generate a log file file_name="some/path/to/file" files_to_backup = read_data_from_file(file_name) for f in files_to_backup: copy_files_to_backup_folder(f) is_backup_successful() write_log_file(file_name):Notice that there is no specific reason why my backup script should be written in Python. I'm not even using what OOP has to offer to create my script because as it is right now, I don't need too. I could have just used Powershell(Windows 7+) or a Bash script(Linux). In a case such as this, where my Python script could easily be transferred to a shell script, is using global variables the way I have a concern?
My script has a simple reason to exist, create a backup, and write a log file, nothing more. For simple tasks such as this, are global variables a problem? I understand that if my problem were to evolve then, I might consider using classes. For example, suppose if I need to transfer my backup directory to a network drive, and also be able to pull the latest backup, and restore, and I also want to add different compression formats. Now my problem is becoming more complex and the use for classes becomes necessary.