I am a newbie going through a byte of python (3.0). This is the first programming language I have ever used. I am stuck at the point where you make a simple program that creates a backup zip file (p.75). I’m running Windows 7 (64 bit) with python 3.1. Prior to this I installed GNUWin32 + sources, and added C:\Program Files(x86)\GnuWin32\bin to my Path enviornmental variable. This is the program:
#!C:\Python31\mystuff
# Filename : my_backup_v1.py
import os
import time
# backing up a couple small files that I made
source = [r'C:\AB\a', r'C:\AB\b']
#my back up directory
target_dir = 'C:\\Backup'
#name of back up file
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "zip -qr {0} {1}".format(target,' '.join(source))
print(zip_command)
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup failed!')
print('source files are', source)
print('target directory is', target_dir)
print('target is', target)
Output:
zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
Backup failed!
source files are ['C:\\AB\\a', 'C:\\AB\\b']
target directory is C:\Backup
target is C:\Backup\20100106143030.zip
The Tutorial includes a little bit troubleshooting advice: copy and paste the zip_command in the python shell prompt to see if that atleast works:
>>> zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
SyntaxError: invalid syntax (<pyshell#17>, line 1)
Since that didn’t work, the tutorial said to read the GNUWin32 manual for additional help. I’ve looked through it and have yet to see anything that will help me out. To see if the zip function is working I did help(zip) and got the following:
>>> help(zip)
Help on class zip in module builtins:
class zip(object)
| zip(iter1 [,iter2 [...]]) --> zip object
|
| Return a zip object whose .__next__() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .__next__()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __next__(...)
| x.__next__() <==> next(x)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object at 0x1E1B8D80>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
Unfortuneatly I can not really understand the ‘help’ yet. However I played around a bit with the zip function to see how it works.
>>> zip (r'C:AB\a')
<zip object at 0x029CE8C8>
So it appears that the zip function works, but I guess I’m not using it correctly. Please help me out, and keep in mind I haven’t had much experience with programming yet. If you would like to view the tutorial you can find it at http://www.swaroopch.com/notes/Python .
The reason “
zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b” failed at the prompt is because in this case, “zip” is supposed to be a command you’re sending the operating system… nothing to do with Python.This is a little confusing, I’m afraid – when you did “
zip(r'C:AB\a')“, you were using Python’s built-inzip()function, which is unrelated to what you’re trying to do.Do you have the proper directory struture? I mean, do C:\AB\a and C:\AB\b exist?
Edit – You should copy/paste that long “zip” line to the command prompt (hit windows key + R, then type “cmd” and hit enter), to see if it works; not the Python shell.