Newbie needs help with Python Tutorial
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 de开发者_如何学Pythonfined 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 www.swaroopch.com/notes/Python .
>>> zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
sounds like a command you should type in your operating system's shell, not in python's shell. Maybe you can try
os.system('zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b')
in the python shell...
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-in zip()
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.
That's not giving you the help for the shell command zip
, it's giving you the help for the python function zip
, which is unrelated to zip file compression.
A couple suggestions for you to use to improve:
Use the subprocess.call([command', 'arg', 'arg', 'arg'])
command to call the external zip command. This will ensure that the command gets called correctly with data escaped properly.
Use os.path.join
to join path componenets together cleanly. This will also help prevent subtle bugs.
Ensure your zip command works from the Command Prompt before trying it in python.
--
Other than that, it looks pretty good.
PS. zip()
is a builtin, used for joining two iterables. Not for zipping files.
Important: see Andrew's comment (copied here for reference):
Looks like you should have done zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
at the terminal prompt, not a python prompt. Does that help? – Andrew McGregor 3 mins ago
精彩评论