Thursday, September 7, 2017

BACK UP THE FILES OR FOLDERS & ALTERNATIVE TO TERNARY OPERATOR IN PYTHON 

We usually back up the file in order to have it safe , Here below is the util program which will help you to back up the files by compressing it and save it . 

We don't have ternary operator in python instead we can use the "if" clause to behave as ternary operator , Here below is the Utility to demonstrate the same.

  import os as osInstance
import time as timeInstance

#get the current working directory in python
currentDirInstance = osInstance.getcwd()

#user can change this to comman line args to pass it from command line
sourceDir = [currentDirInstance]
targetDir = currentDirInstance

#set the target directory to save the date wise back up , if the back up has to be done for two days
target = targetDir + osInstance.sep + timeInstance.strftime('%Y%m%d%H%M%S') + '.zip'

if not osInstance.path.exists(targetDir):
  osInstance.mkdir(targetDir)

zipCommand = 'zip -r {0} {1}'.format(target,' '.join(sourceDir))

#Substuite for ternary operator in Python (using if else as ternary operator in python)
print('backed Up to Target Location :: ', target if osInstance.system(zipCommand) == 0 else 'Backup FAILED')

#or

# Index of the tuple can be used to evaluate this expression
print (('Backup FAILED', 'backed Up to Target Location :: ')[osInstance.system(zipCommand) == 0])

No comments:

Post a Comment