0

Hoping some one can help me out with an issue I have with my first program from the e-book "A byte of Python".

The e-book is based on Python 2.7.

Program: zip a source directory and format it with a particular "Name" and place it in the destination directory.

The E-Books code to do this task is:

import os
import time

# source directory of files to backup
source = ["/root/Documents/test"]

# target location for files to be backed up to
target_dir = "/root/Documents/Backup"

target = target_dir + os.sep

time.strftime("%Y%m%d%H%M") + ".zip"

# if target path does not exist, create it
if not os.path.exists(target_dir):
    os.mkdir(target_dir)

# Command to zip files
zip_command = "zip -r {0}".format(target).join(source)

#run the backup
print "Zip command is:"
print (zip_command)
print "Running"

# Check to see if backup was successful

if os.system(zip_command) == 0:
print "Successfull backup to", target

else:

print "Backup Failed"

The error I am receiving is:

Zip command is:
/root/Documents/test
Running
Backup Failed
sh: 1: /root/Documents/test: Permission denied

Process finished with exit code 0

I have checked the permissions on the folders, attempted to run the code as super user etc.. but I am having no luck, I am extremely new to Python and don't really want to continue on with this e-book until I have found a resolution to this problem as it re-uses and refines this code as I go on.

I have a feeling the problem could be related to the zip_command?

Any help would be greatly appreciated.

1 Answer 1

1

You are constructing the command wrongly!

zip_command = "zip -r {0}".format(target)   +  " "   + " ".join(source)
    #  Result: zip -r /root/Documents/Backup <space>   /root/Documents/test
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for that, I actually typed that part of the code incorrectly. I have changed it exactly how it is in the e-book, I was tampering around with it a bit.
Thank you, that worked perfectly, so simple argh!! thanks for your prompt response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.