[Tutor] copy zip file from source folder to destination and unzipall files within destination
Alan Gauld
alan.gauld at btinternet.com
Fri Nov 20 10:35:26 CET 2009
"MARCUS NG" <markersng at gmail.com> wrote
> Currently nothing works due to my limited understanding.
That's a pretty vague description of the problem.
What exactly happens?
Does the file get copied?
Do you get error messages? If so what?
> I am wondering if my syntax is wrong or am I missing anything?
If the syntax is wrong Python should tell you.
Does it?
The more information you give us the better able we
are to diagnose the problem.
> the code is as such. also if there is a better code, I am definitely open
> to
> it. If it is good, can you also explain steps?
Without any more info I'll pass along a couple of observations.
> ###########
>
> import shutil, zipfile
> shutil.copyfile('C:\Users\blueman\Desktop\myTest1.0.zip',
> 'C:\Users\blueman\Desktop\allFiles')
The first thing is the use of DOS style path names.
You need to do one of the following:
1) Use raw strings - prefix the string with an r
r'C:\Users\blueman\Desktop\allFiles')
2) Escape the backslashes
'C:\\Users\\blueman\\Desktop\\allFiles')
3) Use Unix style slashes:
'C:/Users/blueman/Desktop/allFiles')
Otherwise Python sees the \ as an escape character
and tries to interpret \U, \b \D and \a as special characters
like \n etc
> zipfilepath='C:\Users\blueman\Desktop\allFiles\myTest1.0.zip'
> extractiondir='C:\Users\blueman\Desktop\allFiles\test'
Same here
> def extract(zipfilepath, extractiondir):
> zip = zipfile(zipfilepath)
> zip.extractall(path=extractiondir)
> print 'it works'
While puuting things in functions is usuually a good idea, in this case
you only call it once so I wouldn't bother - at least not until I had the
program working
> extract()
You don't pas any arguments but you defined the functin to
take 2 - Python should tell you about this.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list