Вопрос

I am new to programing python (5 days now) and have ran into a problem that i can't seem to find the answer to in this forum. i would appreciate any help and a detailed explaination would be very much appreciated.

To being. i'm using python 3 on a macbook pro. i'm using an editor called "komodo edit" I'm using CH Swaroop's "Byte of Python" as a guide.

the problem i'm having is with an example of creating a program that takes a file from one folder and backs it up to another as a zip file.

the example in the book is as follows:

Save as backup_ver1.py:
import os
import time
#  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.
#  The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
#  The files are backed up into a zip file.
#  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'
#  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')

my code is as follows

import os
import time
source = ['C:\\learningPython','C:\\scan'] # can i use / instead of the \?
# why is square brackets used here? i'm under the assumption the square brackets are used for list. i'm also assuming one is the directory and the other is the file.
target_dir = 'C:/backup' # created a "backup" folder
target = target_dir + os.sep +time.strftime('%Y%M%D%H%M%S') + '.zip'
zip_command = "zip -qr {0} {1}".format(target, ‚ ‚.join(source))
if os.system(zip_command) == 0:
    print(‚Successful backup to‚, target)
else:
    print(‚Backup FAILED‚)

I get a

zip error: Nothing to do! (try: zip -qr C:/backup/20133201/15/13203219.zip . -i C:learningPythonC:scan) backup failed

Это было полезно?

Решение

The example code you're looking at is totally Windows specific, what with the C:\blah paths. An equivalent path on OS X would look something like:

/Users/yourusername/Documents

(You also seem to have somehow transformed a few 's into ,s when converting the code. Not sure what that's all about.)

Другие советы

As another answerer has noted, the code you're using is Windows-specific and the amendments that the other answer suggested will be required to make it work in OSX.

To diagnose the problem I've made some amendments to your code as follows (changing some commas to apostrophes, changing the strftime() call and adding a print() to show what is actually in zip_command):

import os
import time
source = ['C:\\learningPython','C:\\scan']
target_dir = 'C:/backup'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'   # modified
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
print(zip_command)  # added - see discussion below
if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

When I also created the three folders c:\backup, c:\learningPython and c:\scan and put a file in c:\learningPython so that zip had something to do, the amended script gave the following output on my Windows PC:

C:\usr\sjl\dev\test\python>python ziptest.py
zip -qr C:/backup\20130116152602.zip C:\learningPython C:\scan
Successful backup to C:/backup\20130116152602.zip

... which is, I think, what you were hoping for.

I find that when I'm running system commands from Python it always pays to print the command out, exactly as I'm asking os.system() to run it, so that I know exactly what is going to be run (and so I can try that command from the shell manually if it doesn't do what I think it should do).

I've also modified the format string in strftime() because the original version had a capital 'M' after the 'Y' (which would have shown the number of minutes rather than the number of months) and a capital 'D' instead of a 'd' for the day number. 'D' isn't a valid code letter in a strftime() format string (see here). For me, using Python 3.2, the wrong letter caused a runtime error when I tried to run the script.

With respect to your other question, source is a list of the directories that zip is being asked to process. If you only need to process one directory, you could as easily write the line starting with zip_command as (with the folder name in OS X / Unix style this time):

zip_command = "zip -qr {0} {1}".format(target, '/Users/yourusername/Documents'))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top