python - zip error: Invalid command arguments (cannot write zip file to terminal) -
i learning book bite of python. after typing in example in book
import os import time # 1. files , directories backed # specified in list. # example on windows: # source = ['"c:\\my documents"', 'c:\\code'] # example on mac os x , linux: source = ['/home/username/downloads/books'] # notice had use double quotes inside string # names spaces in it. # 2. backup must stored in # main backup directory # example on windows: # target_dir = 'e:\\backup' # example on mac os x , linux: target_dir = '/home/username/downloads/backup' # remember change folder using # 3. files backed zip file. # 4. name of zip archive current date , time target = target_dir + os.sep + \ time.strftime('%y%m%d%h%m%s') + '.zip' # create target directory if not present if not os.path.exists(target_dir): os.mkdir(target_dir) # make directory # 5. use zip commond put files in zip archive zip_command = "zip - r {0} {1}".format(target, ' '.join(source)) # run backup print "zip command is:" print zip_command print "running:" if os.system(zip_command) == 0: print 'successful backup to', target else: print 'backup falied'
i goe message zip error: invalid command arguments (cannot write zip file terminal)
can not figure out goes wrong, type in same code in book. knows why happen?
the issue in zip
command creating , there space between -
, r
. example -
zip_command = "zip - r {0} {1}".format(target, ' '.join(source)) ^ notice space
there should no space between -
, r
. example -
zip_command = "zip -r {0} {1}".format(target, ' '.join(source))
also, suggest that, better use subprocess.call()
rather os.system
, providing list of arguments command. example -
import subprocess zip_command = ['zip','-r',target] s in source: zip_command.append(s) subprocess.call(zip_command)
it have been easier see error way.
Comments
Post a Comment