4

I have been going through "A byte of Python" to learn the syntax and methods etc...

I have just started with a simple backup script (straight from the book):

#!/usr/bin/python

# Filename: backup_ver1.py

import os

import time

# 1. The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']

# Notice we had to use double quotes inside the string for names with spaces in it.
# 2. The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'


# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))


# Run the backup

if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

Right, it fails. If I run the zip command in the terminal it works fine. I think it fails because the zip_command is never actually run. And I don't know how to run it.

Simply typing out zip_command does not work. (I am using python 3.1)

8
  • Please remember to format your source code. Commented Nov 10, 2009 at 20:30
  • Please assume a hash in front of the bold lettering. Sorry Commented Nov 10, 2009 at 20:32
  • I dont know the formatting yet... Commented Nov 10, 2009 at 20:32
  • I see you added tabs, thanks. Commented Nov 10, 2009 at 20:34
  • If I run the zip command -> exactly what command do you type, is this zip -qr E:\Backup\test.zip "C:\My Documents" C:\\Code? Or just zip (the real question being: do those directory exist on your system)? Also, please mention which OS you are on, Windows presumably but I'd rather be sure. Commented Nov 10, 2009 at 20:45

4 Answers 4

1

It would help us if you could format your code as code; select the code parts, and click on the "Code Sample" button in the editor toolbar. The icon looks like "101/010" and if you hold the mouse pointer over it, the yellow "tool tip" box says "Code Sample <pre></pre> Ctrl+K"

I just tried it, and if you paste code in to the StackOverflow editor, lines with '#' will be bold. So the bold lines are comments. So far so good.

Your strings seem to contain backslash characters. You will need to double each backslash, like so:

target_dir = 'E:\\Backup'

This is because Python treats the backslash specially. It introduces a "backslash escape", which lets you put a quote inside a quoted string:

single_quote = '\''

You could also use a Python "raw string", which has much simpler rules for a backslash. A raw string is introduced by r" or r' and terminated by " or ' respectively. examples:

# both of these are legal
target_dir = r"E:\Backup"
target_dir = r'E:\Backup'
Sign up to request clarification or add additional context in comments.

2 Comments

Well, I added the back slashes and it did not make any difference.
Let me quote the book: Then, we finally run the command using the os.system function which runs the command as if it was run from the system i.e. in the shell - it returns 0 if the command was successfully, else it returns an error number.
0

Are you sure that the Python script is seeing the same environment you have access to when you enter the command manually in the shell? It could be that zip isn't on the path when Python launches the command.

1 Comment

They are in the same path as far as I can see.
0

The next step I recommend is to modify your script to print the command string, and just look at the string and see if it seems correct.

Another thing you can try is to make a batch file that prints out the environment variables, and have Python run that, and see what the environment looks like. Especially PATH.

Here is a suggested example:

set
echo Trying to run zip...
zip

Put those in a batch file called C:\mytest.cmd, and then have your Python code run it:

result_code = os.system("C:\\mytest.cmd")
print('Result of running mytest was code', result_code)

If it works, you will see the environment variables printed out, then it will echo "Trying to run zip...", then if zip runs it will print a message with the version number of zip and how to run it.

2 Comments

I have already done the first part, and it works. So the zip_command works. I don't know how to do the second part though.
What do you not understand about the second part? Here, I'll phrase it a different way: run Windows Notepad, and copy the "suggested example" lines (set, echo, and zip lines above) and paste them in to Notepad. Save as the file "C:\mytest.cmd". Then run the Python code shown: result_code = os.system and the rest.
0

zip command only work in linux not for windows.. thats why it make an error..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.