I found this tutorial in A Byte of Python, but do not understand it:
import os
import time
source = [r'C:\Users\Desktop\Stuff']
target_dir = 'C:\Users\Backup'
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = 'zip -qr "%s" %s' % (target, ' '.join(source))
if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup failed!'
After checking help(os) I do not understand why os.system(zip_command) would ever be zero. .system() does not return a Boolean, does it?
thanks.
help(os.system)says:So, it executes the
commandyou pass as a parameter and returns theexit_statusof that command.When a program returns
0as result, it means it was executed succesfully and if it returns anything else, it probably means there was an error somewhere.So in this line:
you are actually asking: if the command line was executed succesfully then … else …