Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-25 Thread Gregory Lund
> Why did you change file mode to "a"?
I was trying different things and forgot to change it back before I cut/pasted.
>


>
> dest_path = os.path.dirname(fullpath)
> x.extractall(dest_path)
>
Ding ding ding, winner winner chicken dinner!
It's working!
Final .py stand alone code is:

import os, os.path, zipfile, arcpy

in_Zip = r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

z = zipfile.ZipFile(in_Zip,'r')

z.extractall(outDir)

zipContents = z.namelist()
z.close()

for item in zipContents:
if item.endswith('.zip'):
# Combine the base folder name with the subpath to the zip file
fullpath = os.path.join(outDir, item)
x = zipfile.ZipFile(fullpath,'r')
dest_path = os.path.dirname(fullpath)
x.extractall(dest_path)
x.close()

and... the final code that I'll use in my ArcGIS script/tool is:
(I kept the old code with absolute paths to help anyone who wanted to
use this on their zip of zips (with it commented out and my ArcGIS
requirements immediately below.)


import os, os.path, zipfile, arcpy

#in_Zip = r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'
in_Zip = arcpy.GetParameterAsText(0)

#outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"
outDir = os.getcwd()

z = zipfile.ZipFile(in_Zip,'r')

z.extractall(outDir)

zipContents = z.namelist()
z.close()

for item in zipContents:
if item.endswith('.zip'):
# Combine the base folder name with the subpath to the zip file
fullpath = os.path.join(outDir, item)
x = zipfile.ZipFile(fullpath,'r')
dest_path = os.path.dirname(fullpath)
x.extractall(dest_path)
x.close()


Words/pixels can not express how grateful I am to everyone that
pitched in and helped guide me through this seemingly simple task.
The code works & the Esri ArcGIS tool works!

It's not really 'my' code or tool:
the credit goes to Peter, Oscar, Dave, Stephen and others for
their/your comments, debugging, code suggestions, and patience.

I also appreciate the fact that someone didn't just give me the full
code, struggling through googling etc. helped me learn.
I'm still a rookie/neophyte, but a very happy one this morning!
Aug. 7 to now, thanks for all the help!

Thanks again!

Regards,
Greg Lund

PS, but aren't you all going to miss my daily stupid questions? (ok,
maybe not!).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-25 Thread Peter Otten
Gregory Lund wrote:

> if item.endswith('.zip'):
> # Combine the base folder name with the subpath to the zip file
> fullpath = os.path.join(outDir, item)
> x = zipfile.ZipFile(fullpath,'a')

Why did you change file mode to "a"?

> x.extractall()
> x.close()
 

> I tried to use:
> x.extractall(fullpath) but that of course gave me errors because
> 'fullpath' is the path of the file.
> I need to figure out how to just list the respective Lab_2\aforker,
> Lab_2\allisw99 folders.
> 
> Thus far, I have not had any luck.
> Does python have a way to go back to the relative folder where the zip
> is located?

dest_path = os.path.dirname(fullpath)
x.extractall(dest_path)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-24 Thread Gregory Lund
>> Near as I can tell, I got rid of the permissions error, the ZipFile
>> error with the missing capital 'F'
>> Now I need to 'get into' the non zipped folder of each student and
>> unzip any and all zips that are inside of it.
>
>
> The error message says 'No such file or directory'. That means that when
> Python tries to open a file with the name you gave it can't find a file that
> has that name. In this case I suspect the problem is that you need to give
> the full path to each file i.e. instead of
>   'Lab_2/aforker/aforker_Lab2.zip'

full code is now:


import os, os.path, zipfile, arcpy

in_Zip = r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

z = zipfile.ZipFile(in_Zip,'r')

z.extractall(outDir)

zipContents = z.namelist()
print zipContents
z.close()

for item in zipContents:
if item.endswith('.zip'):
# Combine the base folder name with the subpath to the zip file
fullpath = os.path.join(outDir, item)
x = zipfile.ZipFile(fullpath,'a')
x.extractall()
x.close()

NO errors! yea!
But, it's (I'm) still just extracting the first zip, not the zipfiles
within the Lab_2\aforker, Lab_2\allisw99 folders.

however, I used my brain (no comments!) and figured out that while
it's not extracting the info to the folders I want (the Lab_2\aforker,
Lab_2\allisw99 etc. folder, it's extracting them to the location in
which my script is stored.
I kept looking at it thinking 'OK, it didn't come up with an error,
but it's 'not' extracting, but it should be then I realized that I
didn't specify the folder to which I want it extracted.
Ta-da, that's why it's going back to where the script is stored.

So...
I tried to use:
x.extractall(fullpath) but that of course gave me errors because
'fullpath' is the path of the file.
I need to figure out how to just list the respective Lab_2\aforker,
Lab_2\allisw99 folders.

Thus far, I have not had any luck.
Does python have a way to go back to the relative folder where the zip
is located?

> I just googled to find you a page explaining absolute paths and, by chance,
> came up with this from the ArcGIS documentation:
> http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Pathnames%20explained%3A%20Absolute%2C%20relative%2C%20UNC%2C%20and%20URL

Good find, It's like pulling teeth to explain paths/directories to my students.
The relative vs. absolute paths is one of their first obstacles to
learning, yet really it's a simple check box in ArcGIS that is NOT a
default, and resets each night in the computer lab.

I am going to use that link you provided, tomorrow, the first day of GIS311!

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-24 Thread Oscar Benjamin
On 25 September 2012 00:33, Gregory Lund  wrote:
>
> z.extractall(outDir)
>
> zipContents = z.namelist()
> print zipContents
> z.close()
>
> for item in zipContents:
> if item.endswith('.zip'):
> x = zipfile.ZipFile(item,'r')
> x.extractall()
> x.close()
>
> Traceback (most recent call last):
>   File
> "D:/D_Drive_Documents/Scripts/Unzip_a_zip_of_zips/Scripts/unzip_a_zip_of_zips_rewrite_shortest_of_the_shorts2.py",
> line 18, in 
> x = zipfile.ZipFile(item,'r')
>   File "C:\Python26\ArcGIS10.0\lib\zipfile.py", line 683, in __init__
> self.fp = open(file, modeDict[mode])
> IOError: [Errno 2] No such file or directory:
> 'Lab_2/aforker/aforker_Lab2.zip'
> >>>
>
> Near as I can tell, I got rid of the permissions error, the ZipFile
> error with the missing capital 'F'
> Now I need to 'get into' the non zipped folder of each student and
> unzip any and all zips that are inside of it.
>

The error message says 'No such file or directory'. That means that when
Python tries to open a file with the name you gave it can't find a file
that has that name. In this case I suspect the problem is that you need to
give the full path to each file i.e. instead of
  'Lab_2/aforker/aforker_Lab2.zip'
you need to give
 
'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\Lab_2\aforker\aforker_Lab2.zip'

You can create the full path with:

import os.path   # Put this at the top of your file

for item in zipContents:
  if item.endswith('.zip'):
  # Combine the base folder name with the subpath to the zip file
  fullpath = os.path.join(outDir, item)
  x = zipfile.ZipFile(fullpath,'r')

I just googled to find you a page explaining absolute paths and, by chance,
came up with this from the ArcGIS documentation:
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Pathnames%20explained%3A%20Absolute%2C%20relative%2C%20UNC%2C%20and%20URL

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-24 Thread Gregory Lund
> No but Python did when it ran your code and it tried to tell you. The trick
> is to read the error message, see what line of code it occurs at and then
> look very closely at that line of code and the surrounding lines of code.
> The first thing to check for is a typo.

To be honest, I did check for typos, a misplaced period, etc., but
didn't notice it, I even compared it with the similar line above, but
didn't notice the 'F'.
(Truth Hurts)
>
> The error message that Python gives may seem cryptic but it's actually very
> informative if you know how to read it. For this reason it's also more
> helpful to show the *verbatim* error message when posting to this (or other)
> lists.
>
I will show the verbatim error message from now on, thank you for suggesting it!
, speaking of which:

This is my code, as modified based on all the great help thus far.

(again, I need a stand alone .py file for my purposes and need to use
Python 2.6 because it's the version that works with my GIS application
(Esri's ArcGIS).


import os, os.path, zipfile, arcpy

in_Zip = r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

z = zipfile.ZipFile(in_Zip,'r')

z.extractall(outDir)

zipContents = z.namelist()
print zipContents
z.close()

for item in zipContents:
if item.endswith('.zip'):
x = zipfile.ZipFile(item,'r')
x.extractall()
x.close()

for the record, I print  'zipContents' to help me figure out how to
'get into' the first folder of the extracted zip which is never a zip
folder, but a non-zipped folder, that holds (usually) 1 or more .zip
files (folders that are zipped)

This is the way I receive the data from the Learning Management System
LMS). The initial zip (2012-09-18 Lab_2.zip) is created by the LMS,
and actually the internal folders (aforker, allisw99, etc.) are also
created by the LMS (its' how the LMS sorts the individual student
folders/upload using their user name.

my results and error of the above code is listed below.

IDLE 2.6.5
>>>  RESTART 
>>>
['Lab_2/aforker/', 'Lab_2/aforker/aforker_Lab2.zip',
'Lab_2/allisw99/', 'Lab_2/allisw99/allisw99_Lab2.zip',
'Lab_2/allisw99/allisw99_Lab2_Bonus.pdf', 'Lab_2/allisw992/',
'Lab_2/allisw992/allisw99_Lab2_Bonus.pdf', 'Lab_2/btaylor7/',
'Lab_2/btaylor7/2nd_btaylor7_Lab2.zip',
'Lab_2/btaylor7/btaylor7_Lab2.zip', 'Lab_2/']

Traceback (most recent call last):
  File 
"D:/D_Drive_Documents/Scripts/Unzip_a_zip_of_zips/Scripts/unzip_a_zip_of_zips_rewrite_shortest_of_the_shorts2.py",
line 18, in 
x = zipfile.ZipFile(item,'r')
  File "C:\Python26\ArcGIS10.0\lib\zipfile.py", line 683, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'Lab_2/aforker/aforker_Lab2.zip'
>>>

Near as I can tell, I got rid of the permissions error, the ZipFile
error with the missing capital 'F'
Now I need to 'get into' the non zipped folder of each student and
unzip any and all zips that are inside of it.

Thoughts?
Thanks again Oscar and Dave for sticking with me and not throwing in
the towel on my rookie errors!

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-24 Thread Oscar Benjamin
On 24 September 2012 22:15, Gregory Lund  wrote:

> >> but now get a   "  x = zipfile.Zipfile(item,'r')
> >> AttributeError: 'module' object has no attribute 'Zipfile' "
> >>
> >> error
> >>
> >> gr, this is going to send me to the funny farm!
> >>
> >> greg
> >>
> > One way to avoid the "funny farm" is to learn to use the tools that
> > Python provides, built-in.  As soon as you get an error like that one,
> > add a print statement immediately before the one in error, and find the
> > type and attributes of the object that supposedly doesn't have the
> > attribute.  For example, you could have added a dir(zipfile), and then
> > studied the one that seems to be the same as the one you tried to use.
> > Presumably you would have quickly discovered what Oscar pointed out.
> >
> Thank you, I have printed and added to my Python quick hints.
>
> > We all make typos, the difference is in how quickly we find and fix
> > them.  Use the tools.
> >
> must admit, I didn't notice the typo.
>

No but Python did when it ran your code and it tried to tell you. The trick
is to read the error message, see what line of code it occurs at and then
look very closely at that line of code and the surrounding lines of code.
The first thing to check for is a typo.

The error message that Python gives may seem cryptic but it's actually very
informative if you know how to read it. For this reason it's also more
helpful to show the *verbatim* error message when posting to this (or
other) lists.

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-24 Thread Gregory Lund
>
> Capital F. ZipFile not Zipfile.
>
Keyboard - Forehead - SCHMACK.
(and hand/forehead smack.)

DOH!

Thank you.

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-24 Thread Gregory Lund
>> but now get a   "  x = zipfile.Zipfile(item,'r')
>> AttributeError: 'module' object has no attribute 'Zipfile' "
>>
>> error
>>
>> gr, this is going to send me to the funny farm!
>>
>> greg
>>
> One way to avoid the "funny farm" is to learn to use the tools that
> Python provides, built-in.  As soon as you get an error like that one,
> add a print statement immediately before the one in error, and find the
> type and attributes of the object that supposedly doesn't have the
> attribute.  For example, you could have added a dir(zipfile), and then
> studied the one that seems to be the same as the one you tried to use.
> Presumably you would have quickly discovered what Oscar pointed out.
>
Thank you, I have printed and added to my Python quick hints.

> We all make typos, the difference is in how quickly we find and fix
> them.  Use the tools.
>
must admit, I didn't notice the typo.
> --
Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Dave Angel
On 09/23/2012 10:26 PM, Gregory Lund wrote:
> 
>
> for item in zipContents:
> if item.endswith('.zip'):
> x = zipfile.Zipfile(item,'r')
> x.extractall()
> x.close()
>
> but now get a   "  x = zipfile.Zipfile(item,'r')
> AttributeError: 'module' object has no attribute 'Zipfile' "
>
> error
>
> gr, this is going to send me to the funny farm!
>
> greg
>
One way to avoid the "funny farm" is to learn to use the tools that
Python provides, built-in.  As soon as you get an error like that one,
add a print statement immediately before the one in error, and find the
type and attributes of the object that supposedly doesn't have the
attribute.  For example, you could have added a dir(zipfile), and then
studied the one that seems to be the same as the one you tried to use. 
Presumably you would have quickly discovered what Oscar pointed out.

We all make typos, the difference is in how quickly we find and fix
them.  Use the tools.

-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Oscar Benjamin
On 24 September 2012 03:26, Gregory Lund  wrote:

> >
> > but a better way to do that is:
> > if item.endswith('.zip')
> >
> >> x = zipfile.Zipfile(item,'r')
> >> x.extractall()
> >> x.close()
> >>
> ok, I used that (above)
> typed below:
>
> for item in zipContents:
> if item.endswith('.zip'):
> x = zipfile.Zipfile(item,'r')
> x.extractall()
> x.close()
>
> but now get a   "  x = zipfile.Zipfile(item,'r')
> AttributeError: 'module' object has no attribute 'Zipfile' "
>

Capital F. ZipFile not Zipfile.


>
> error
>
> gr, this is going to send me to the funny farm!
>
> greg
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Gregory Lund
>
> but a better way to do that is:
> if item.endswith('.zip')
>
>> x = zipfile.Zipfile(item,'r')
>> x.extractall()
>> x.close()
>>
ok, I used that (above)
typed below:

for item in zipContents:
if item.endswith('.zip'):
x = zipfile.Zipfile(item,'r')
x.extractall()
x.close()

but now get a   "  x = zipfile.Zipfile(item,'r')
AttributeError: 'module' object has no attribute 'Zipfile' "

error

gr, this is going to send me to the funny farm!

greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Oscar Benjamin
On 24 September 2012 03:00, Gregory Lund  wrote:

> >>> > You're trying to open the directory as if it were a zipfile. You
> should
> >>> > pass in the path to a zipfile not to a folder.
> >>>
> >>> Agreed, but how do I do that "pass in the path to a zipfile'?
> >>> Using an if statement I presume?
> >>>
> >>> > >
> >>>
> >>> > > I guess my first issue is to resolve the 'Permission denied'
> problem.
> >>> > > And, I know I need an 'if' statement, somehow...
> >>> >
> >>> Yes, the permission denied problem seems to be the first real
> >>> obstacle, but I can't figure it out.
> >>> I tried searching online but everything I found to try, didn't work.
> >>
> >>
> >> The permission error is because you are trying to open a folder as if it
> >> were a file. The operating system does not give you permission to do
> this
> >> because it is a nonsensical operation.
> >>
> >>> > Remove the folder paths from the list of paths.
> >>>
> >>> How can I remove the folder paths from the list of paths?
> >>
> >>
> >> You can do it with a list comprehension:
> >>
> >> import os.path
> >> paths = [p for p in paths if os.path.isdir(p)]
> >
> >
> > Sorry that should be (note the 'not'):
> > paths = [p for p in paths if not os.path.isdir(p)]
> >
> Ok, but where do I put that?
> Sorry Oscar, I don't know where to put it, nor do I know how to use
> 'paths' once I do that.
>
> I was trying this prior to getting your email about paths...(I put
> this after z.close()
>
> for item in zipContents:
> if item(-4) == ('.zip'):
>

That should be:
if item[-4:] == '.zip'

but a better way to do that is:
if item.endswith('.zip')

x = zipfile.Zipfile(item,'r')
> x.extractall()
> x.close()
>
> but, I kept getting a "TypeError: 'str' object is not callable" error.
>
> then I tried
>
> for item in zipContents:
> if item.lower() .endswith(".zip"):
>

Ok that's better.


> item.extractall()
> item.close()
>
> but got a "item.extractall()
> AttributeError: 'str' object has no attribute 'extractall'" error
>

item is a string object. You need to use that string to create a ZipFile
object and then call extractall() on the ZipFile object (the way you did in
your original post in this thread).

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Gregory Lund
>>> > You're trying to open the directory as if it were a zipfile. You should
>>> > pass in the path to a zipfile not to a folder.
>>>
>>> Agreed, but how do I do that "pass in the path to a zipfile'?
>>> Using an if statement I presume?
>>>
>>> > >
>>>
>>> > > I guess my first issue is to resolve the 'Permission denied' problem.
>>> > > And, I know I need an 'if' statement, somehow...
>>> >
>>> Yes, the permission denied problem seems to be the first real
>>> obstacle, but I can't figure it out.
>>> I tried searching online but everything I found to try, didn't work.
>>
>>
>> The permission error is because you are trying to open a folder as if it
>> were a file. The operating system does not give you permission to do this
>> because it is a nonsensical operation.
>>
>>> > Remove the folder paths from the list of paths.
>>>
>>> How can I remove the folder paths from the list of paths?
>>
>>
>> You can do it with a list comprehension:
>>
>> import os.path
>> paths = [p for p in paths if os.path.isdir(p)]
>
>
> Sorry that should be (note the 'not'):
> paths = [p for p in paths if not os.path.isdir(p)]
>
Ok, but where do I put that?
Sorry Oscar, I don't know where to put it, nor do I know how to use
'paths' once I do that.

I was trying this prior to getting your email about paths...(I put
this after z.close()

for item in zipContents:
if item(-4) == ('.zip'):
x = zipfile.Zipfile(item,'r')
x.extractall()
x.close()

but, I kept getting a "TypeError: 'str' object is not callable" error.

then I tried

for item in zipContents:
if item.lower() .endswith(".zip"):
item.extractall()
item.close()

but got a "item.extractall()
AttributeError: 'str' object has no attribute 'extractall'" error

Can't say I'm not trying. :-)
not working, but i'm trying whatever I can.

I greatly appreciate the help Oscar!

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Oscar Benjamin
On 24 September 2012 02:34, Oscar Benjamin wrote:

> On 24 September 2012 02:22, Gregory Lund  wrote:
>
>
>>  > You're trying to open the directory as if it were a zipfile. You should
>> > pass in the path to a zipfile not to a folder.
>>
>> Agreed, but how do I do that "pass in the path to a zipfile'?
>> Using an if statement I presume?
>>
>> > >
>>
>> > > I guess my first issue is to resolve the 'Permission denied' problem.
>> > > And, I know I need an 'if' statement, somehow...
>> >
>> Yes, the permission denied problem seems to be the first real
>> obstacle, but I can't figure it out.
>> I tried searching online but everything I found to try, didn't work.
>>
>
> The permission error is because you are trying to open a folder as if it
> were a file. The operating system does not give you permission to do this
> because it is a nonsensical operation.
>
> > Remove the folder paths from the list of paths.
>>
>> How can I remove the folder paths from the list of paths?
>>
>
> You can do it with a list comprehension:
>
> import os.path
> paths = [p for p in paths if os.path.isdir(p)]
>

Sorry that should be (note the 'not'):
paths = [p for p in paths if not os.path.isdir(p)]

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Oscar Benjamin
On 24 September 2012 02:22, Gregory Lund  wrote:


> > You're trying to open the directory as if it were a zipfile. You should
> > pass in the path to a zipfile not to a folder.
>
> Agreed, but how do I do that "pass in the path to a zipfile'?
> Using an if statement I presume?
>
> > >
> > > I guess my first issue is to resolve the 'Permission denied' problem.
> > > And, I know I need an 'if' statement, somehow...
> >
> Yes, the permission denied problem seems to be the first real
> obstacle, but I can't figure it out.
> I tried searching online but everything I found to try, didn't work.
>

The permission error is because you are trying to open a folder as if it
were a file. The operating system does not give you permission to do this
because it is a nonsensical operation.

> Remove the folder paths from the list of paths.
>
> How can I remove the folder paths from the list of paths?
>

You can do it with a list comprehension:

import os.path
paths = [p for p in paths if os.path.isdir(p)]

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Gregory Lund
> >
> > z.close
>
> You need to have round brackets to actually call the close method, i.e.:
>
> z.close()
>
Fixed

> > 
> >
> > It works, I get the following in the Python Shell:
> >
> > '>>>  RESTART
> > '
> >
> > '>>>'
> >
> > ['Lab_2/aforker/', 'Lab_2/aforker/aforker_Lab2.zip',
> > 'Lab_2/allisw99/', 'Lab_2/allisw99/allisw99_Lab2.zip',
> > 'Lab_2/allisw99/allisw99_Lab2_Bonus.pdf', 'Lab_2/allisw992/',
> > 'Lab_2/allisw992/allisw99_Lab2_Bonus.pdf', 'Lab_2/btaylor7/',
> > 'Lab_2/btaylor7/2nd_btaylor7_Lab2.zip',
> > 'Lab_2/btaylor7/btaylor7_Lab2.zip', 'Lab_2/']
>
> Did you mean to have the paths of folders (e.g. 'Lab_2/aforker') in the
> same list as the paths of zip-files (e.g. 'Lab_2/aforker/aforker_Lab2.zip')?
>
Actually, I just used the 'zipContents = z.namelist() to see if I
could get some sort of positive result of my initial unzip.
It was just so I could see what I was getting.
but, I see that you're correct in noticing that i do use that namelist
in the next block of code, which (as you point out) isn't going to
work.

> >
> > '>>> '
> >
> > But, what I can't figure out is how to get 'into' each unique folder:
> > aforker, allisw99, etc. and then extract any and all zips within
> > 'aforker', 'allisw99', etc.
> >
> > I have no doubt that the suggestions posted here will work, but they
> > all seem to get away from a single .py file and
> >  a) I can't get them to work, and
> > b) it doesn't really help me because I need ONE stand alone .py file
> > to make this all work in ArcGIS.
>
> I don't know what ArcGIS is but if it runs Python then I'm sure that it
> can be done with a stand alone .py file.
>
ArcGIS is Esri's ArcGIS Analysis/Mapping software, that uses python
for it's scripts.
And, I hope that you are correct, (that I can get one stand alone .py
file to work.
If I can, I KNOW I can make the minor modifications necessary to use
in the graphic interface.
> >
> >  I will be using this to create an ArcGIS 'tool' that requires one
> > script (at least for me to comprehend it) :-)
> >
> > Thank you in advance for any and all suggestions, tips etc.
> >
> > For the record, I did try the following  (to at least try
> > something) at  the bottom of the code above:
> > ---
> >
> > for item in zipContents:
> > itemLoc = os.path.join(outDir,item)
> > y = zipfile.ZipFile(itemLoc,'a')
> > y.extractall(os.path.aplit(itemLoc)[0])
> > y.close
> > 
> >
> > but I get the following error:
> >
> > Traceback (most recent call last): File
> >
> > "D:\D_Drive_Documents\Scripts\Unzip_a_zip_of_zips\Scripts\unzip_a_zip.py",
> > line 50, in y = zipfile.ZipFile(itemLoc,'a') File
> > "C:\Python26\ArcGIS10.0\lib\zipfile.py", line 687, in init self.fp =
> > open(file, modeDict[mode]) IOError: [Errno 13] Permission denied:
> > 'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\Lab_2/aforker/'
>
> You're trying to open the directory as if it were a zipfile. You should
> pass in the path to a zipfile not to a folder.

Agreed, but how do I do that "pass in the path to a zipfile'?
Using an if statement I presume?
>
> Also you're opening the files in append mode 'a'. I think you really want
> read-only mode 'r'.
>
fixed it to 'r' mode. (rookie mistake)


> >
> > I guess my first issue is to resolve the 'Permission denied' problem.
> > And, I know I need an 'if' statement, somehow...
>
Yes, the permission denied problem seems to be the first real
obstacle, but I can't figure it out.
I tried searching online but everything I found to try, didn't work.

> Remove the folder paths from the list of paths.

How can I remove the folder paths from the list of paths?
>
> Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-23 Thread Oscar Benjamin
On Sep 21, 2012 8:10 PM, "Gregory Lund"  wrote:
> print zipContents
>
> z.close

You need to have round brackets to actually call the close method, i.e.:

z.close()

> 
>
> It works, I get the following in the Python Shell:
>
> '>>>  RESTART
'
>
> '>>>'
>
> ['Lab_2/aforker/', 'Lab_2/aforker/aforker_Lab2.zip',
> 'Lab_2/allisw99/', 'Lab_2/allisw99/allisw99_Lab2.zip',
> 'Lab_2/allisw99/allisw99_Lab2_Bonus.pdf', 'Lab_2/allisw992/',
> 'Lab_2/allisw992/allisw99_Lab2_Bonus.pdf', 'Lab_2/btaylor7/',
> 'Lab_2/btaylor7/2nd_btaylor7_Lab2.zip',
> 'Lab_2/btaylor7/btaylor7_Lab2.zip', 'Lab_2/']

Did you mean to have the paths of folders (e.g. 'Lab_2/aforker') in the
same list as the paths of zip-files (e.g. 'Lab_2/aforker/aforker_Lab2.zip')?


>
> '>>> '
>
> But, what I can't figure out is how to get 'into' each unique folder:
> aforker, allisw99, etc. and then extract any and all zips within
> 'aforker', 'allisw99', etc.
>
> I have no doubt that the suggestions posted here will work, but they
> all seem to get away from a single .py file and
>  a) I can't get them to work, and
> b) it doesn't really help me because I need ONE stand alone .py file
> to make this all work in ArcGIS.

I don't know what ArcGIS is but if it runs Python then I'm sure that it can
be done with a stand alone .py file.

>
>  I will be using this to create an ArcGIS 'tool' that requires one
> script (at least for me to comprehend it) :-)
>
> Thank you in advance for any and all suggestions, tips etc.
>
> For the record, I did try the following  (to at least try
> something) at  the bottom of the code above:
> ---
>
> for item in zipContents:
> itemLoc = os.path.join(outDir,item)
> y = zipfile.ZipFile(itemLoc,'a')
> y.extractall(os.path.aplit(itemLoc)[0])
> y.close
> 
>
> but I get the following error:
>
> Traceback (most recent call last): File
> "D:\D_Drive_Documents\Scripts\Unzip_a_zip_of_zips\Scripts\unzip_a_zip.py",
> line 50, in y = zipfile.ZipFile(itemLoc,'a') File
> "C:\Python26\ArcGIS10.0\lib\zipfile.py", line 687, in init self.fp =
> open(file, modeDict[mode]) IOError: [Errno 13] Permission denied:
> 'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\Lab_2/aforker/'

You're trying to open the directory as if it were a zipfile. You should
pass in the path to a zipfile not to a folder.

Also you're opening the files in append mode 'a'. I think you really want
read-only mode 'r'.

>
> I guess my first issue is to resolve the 'Permission denied' problem.
> And, I know I need an 'if' statement, somehow...

Remove the folder paths from the list of paths.

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-21 Thread Gregory Lund
>
> You need to add the folder where python.exe is to your PATH variable.

I just realized the path we're heading down here isn't going to work.
I need on .py file to be up an running so that I can add it to my tool
in ArcGIS.


>
> Oscar

I appreciate the help Oscar, and I believe it would work that way, but...

I need on .py file to be up an running so that I can add it to my tool
in ArcGIS.

I would like to revise my question and add some more specifics.

I must use Python 2.6!
This is a task that I really want to automate, the situation is static
and once I create this, I'll be golden (good).
I must create one, stand alone script (in Idle, I hope) that will:

1. Unzip a single original zipfile (in_Zip) to the contents of the
folder that the zipfile currently resides.
2. Go to the unique (NON ZIPPED) folders (actually student usernames
'aforker', 'allisw99', 'btaylor7', etc) that result from step 1.
(there may be anywhere from 1 to 40 of these unique student folders)
3. Within each unique folder ('aforker', 'allisw99', 'btaylor7', etc)
extract any and all (could be none, could be 3 or 4) .zip files
within, to their relative aforementioned unique folders ('aforker',
'allisw99', 'btaylor7', etc), while 'navigating' i.e. not getting hung
up on possible .pdf or docx files that may or may not reside in the
unique folders ('aforker', 'allisw99', 'btaylor7', etc)

This is what I've got so far: (and it 'works') (I'll modify later so
that I do not need to hard-code the original zipfile (in_Zip))
---

import os, os.path, zipfile inZip =
r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

z = zipfile.ZipFile(in_Zip,'a')

z.extractall(outDir)

zipContents = z.namelist()

print zipContents

z.close


It works, I get the following in the Python Shell:

'>>>  RESTART '

'>>>'

['Lab_2/aforker/', 'Lab_2/aforker/aforker_Lab2.zip',
'Lab_2/allisw99/', 'Lab_2/allisw99/allisw99_Lab2.zip',
'Lab_2/allisw99/allisw99_Lab2_Bonus.pdf', 'Lab_2/allisw992/',
'Lab_2/allisw992/allisw99_Lab2_Bonus.pdf', 'Lab_2/btaylor7/',
'Lab_2/btaylor7/2nd_btaylor7_Lab2.zip',
'Lab_2/btaylor7/btaylor7_Lab2.zip', 'Lab_2/']

'>>> '

But, what I can't figure out is how to get 'into' each unique folder:
aforker, allisw99, etc. and then extract any and all zips within
'aforker', 'allisw99', etc.

I have no doubt that the suggestions posted here will work, but they
all seem to get away from a single .py file and
 a) I can't get them to work, and
b) it doesn't really help me because I need ONE stand alone .py file
to make this all work in ArcGIS.

 I will be using this to create an ArcGIS 'tool' that requires one
script (at least for me to comprehend it) :-)

Thank you in advance for any and all suggestions, tips etc.

For the record, I did try the following  (to at least try
something) at  the bottom of the code above:
---

for item in zipContents:
itemLoc = os.path.join(outDir,item)
y = zipfile.ZipFile(itemLoc,'a')
y.extractall(os.path.aplit(itemLoc)[0])
y.close


but I get the following error:

Traceback (most recent call last): File
"D:\D_Drive_Documents\Scripts\Unzip_a_zip_of_zips\Scripts\unzip_a_zip.py",
line 50, in y = zipfile.ZipFile(itemLoc,'a') File
"C:\Python26\ArcGIS10.0\lib\zipfile.py", line 687, in init self.fp =
open(file, modeDict[mode]) IOError: [Errno 13] Permission denied:
'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\Lab_2/aforker/'

I guess my first issue is to resolve the 'Permission denied' problem.
And, I know I need an 'if' statement, somehow...

thanks in advance for any and all input!

Greg

PS, neophyte/rookie, trying to keep it simple. This is not my normal
daily task (good thing, right?)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-20 Thread Gregory Lund
>
> You need to add the folder where python.exe is to your PATH variable.
>
> First you need to find the folder where python is on your computer. You can
> do this from within a python script using the following two lines:
> import sys
> print sys.executable
>
> Once you've found the folder containing python.exe, add that folder to your
> PATH variable:
> http://code.google.com/p/tryton/wiki/AddingPythonToWindowsPath
> http://showmedo.com/videotutorials/video?name=96&fromSeriesID=96

Thanks, I'll give it a shot tomorrow morning.
Brain is fried right now.
Greg
>
> Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Oscar Benjamin
On 19 September 2012 19:00, Gregory Lund  wrote:

> >>> and then run it from the shell like this:
> On Wed, Sep 19, 2012 at 10:20 AM, Peter Otten <__pete...@web.de> wrote:
> > Gregory Lund wrote:
>
> >>> and then run it from the shell like this:
> ahh, windows shell, not python shell.
>
> from the directions Peter linked to, I shift-right clicked on the
> folder where my script resided
> (hope that was right)
> then after
> D:\D_Drive_Documents\Scripts\Unzip_a_zip_of_zips\Scripts>
> I typed:python unzip_twice.py 2012-09-18 Lab_2.zip
> Student_Work_Sample_usecopy1
> where:
> unzip_twice.py is the script name as suggested by Steve
> 2012-09-18 Lab_2.zip is the zipfile
> Student_Work_Sample_usecopy1 is the folder the zipfile resides and
> where I want the info extracted to.
>
> Lucky me... NOT...:-)
> I got:
> "'python' is not recognized as an internal or external command,
> operable program or batch file" (without the quotes of course)
>

You need to add the folder where python.exe is to your PATH variable.

First you need to find the folder where python is on your computer. You can
do this from within a python script using the following two lines:
import sys
print sys.executable

Once you've found the folder containing python.exe, add that folder to your
PATH variable:
http://code.google.com/p/tryton/wiki/AddingPythonToWindowsPath
http://showmedo.com/videotutorials/video?name=96&fromSeriesID=96

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Gregory Lund
On Wed, Sep 19, 2012 at 10:40 AM, Peter Otten <__pete...@web.de> wrote:
> Steven D'Aprano wrote:
>
>> That's what we're here for! Don't be shy about asking questions.
>
> Indeed. Also, Gregory, don't expect anything to work directly. Programming
> is mostly an iterative process where you write just a little bit of code
> before you have to stop to fix a host of bugs. Repeat until you have
> something that almost does what you intended...

Believe me, I don't expect it to work directly, that's for sure.
I have just that, a little bit of code that extracts the first zip,
and am now working on a little bit of code to extract the zips within
it.
>
>> However, I have to say Peter was a bit optimistic in his assumptions about
>> your general level of expertise. He mixed Python code and (probably) Linux
>> shell commands and output, which probably didn't help.
>
> Gregory introduced himself as a university lecturer, so I tacitly assumed
> that he had seen a shell window on a linux box or a mac, and would
> appreciate if he could use something familiar (globbing) in a new
> environment (python).

You were correct, I have seen a shell window on a Windows box. Correct
Assumption.
However, I did not know it was that, to which you were referencing,
and it wouldn't of worked where I want it to:
In a GUI via ArcGIS.
>
> I make similar assumptions about questioners' skills level all the time --
> and sometimes fail in a big way ;)
Sorry, tried to emphasize the 'neophyte' from the get go.
Definition of crazy? Doing the same thing over and over again while
expecting different results?

Perhaps the code can not be written to run in a python shell (from
which I could use in my Esri ArcGIS GUI.
(again, I have a spot to put in one link to the .py code, the GUI asks
me (in a way) "which zipfile", I navigate to it and click 
I have no need for shortened code or running in the cmd window, it's
such a short operation, it doesn't matter if it takes me 100 to 1000
lines of 'baby' or 'rookie' code to write, as long as it works.
With only one starting zip and within it up to 40 folders each
containing (usually) one zipfile (max of 5), the matter of time it
takes to run is not an issue.
I just need to have one .py file that does it all, or calls upon other
.py files I guess in order to work.

I do appreciate the help, and am learning in this process.
again, maybe I want python to do something that can't be done?

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Gregory Lund
>>> and then run it from the shell like this:
On Wed, Sep 19, 2012 at 10:20 AM, Peter Otten <__pete...@web.de> wrote:
> Gregory Lund wrote:

>>> and then run it from the shell like this:
ahh, windows shell, not python shell.

from the directions Peter linked to, I shift-right clicked on the
folder where my script resided
(hope that was right)
then after
D:\D_Drive_Documents\Scripts\Unzip_a_zip_of_zips\Scripts>
I typed:python unzip_twice.py 2012-09-18 Lab_2.zip Student_Work_Sample_usecopy1
where:
unzip_twice.py is the script name as suggested by Steve
2012-09-18 Lab_2.zip is the zipfile
Student_Work_Sample_usecopy1 is the folder the zipfile resides and
where I want the info extracted to.

Lucky me... NOT...:-)
I got:
"'python' is not recognized as an internal or external command,
operable program or batch file" (without the quotes of course)

I am still worried that none of this will transfer to my GUI after I
get it working.
Is there a way to do this using the code that I started so that it
will work without the command prompt?
I got part of it to work, as detailed earlier, but... not the second
round of zips.

Maybe it can't be done?

Thanks for the continued help.
either way, once I get it to finally work, it will save me time.

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Peter Otten
Steven D'Aprano wrote:

> That's what we're here for! Don't be shy about asking questions.

Indeed. Also, Gregory, don't expect anything to work directly. Programming 
is mostly an iterative process where you write just a little bit of code 
before you have to stop to fix a host of bugs. Repeat until you have 
something that almost does what you intended...
 
> However, I have to say Peter was a bit optimistic in his assumptions about
> your general level of expertise. He mixed Python code and (probably) Linux
> shell commands and output, which probably didn't help.

Gregory introduced himself as a university lecturer, so I tacitly assumed 
that he had seen a shell window on a linux box or a mac, and would 
appreciate if he could use something familiar (globbing) in a new 
environment (python). 

I make similar assumptions about questioners' skills level all the time -- 
and sometimes fail in a big way ;)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Peter Otten
Gregory Lund wrote:

>> Using Peter's code, if you create a plain text file called
>> "unzip_twice.py" containing:
>>
>>
>> import glob
>> import os
>> import sys
>> import zipfile
>>
>> source_file = sys.argv[1]
>> dest_folder = sys.argv[2]
>>
>> zipfile.ZipFile(source_file).extractall(dest_folder)
>>
>> inner_zips_pattern = os.path.join(dest_folder, "*.zip")
>> for filename in glob.glob(inner_zips_pattern):
>> inner_folder = filename[:-4]
>> zipfile.ZipFile(filename).extractall(inner_folder)
>>
> Consider it done, i have a new .py file saved as 'unzip_twice.py'
> pasted below:
> 
> import glob
> import os
> import sys
> import zipfile
> 
> source_file = sys.argv[1]
> dest_folder = sys.argv[2]
> 
> zipfile.ZipFile(source_file).extractall(dest_folder)
> 
> inner_zips_pattern = os.path.join(dest_folder, "*.zip")
> for filename in glob.glob(inner_zips_pattern):
> inner_folder = filename[:-4]
> zipfile.ZipFile(filename).extractall(inner_folder)
> 
>>
>> and then run it from the shell like this:
>>
>> python unzip_twice.py NAME-OF-ZIP-FILE NAME-OF-FOLDER-TO-EXTRACT-TO
>>
> In the IDLE 2.6.5 shell I typed (exactly what is in quotes, without

Don't use Idle, follow the instructions on

http://www.windows7hacker.com/index.php/2009/08/how-to-open-dos-prompt-command-here-in-windows-7-and-more/

If you see the ugly little black window you are there an can type in your 
command.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Gregory Lund
more info:


> Consider it done, i have a new .py file saved as 'unzip_twice.py'
but it would be easier (the whole purpose of this task) is to have a
stand alone script that would work without having to open up the
shell.
In ArcGIS, I call the script by double clicking on my tool, selecting
the .zip in a GUI and it runs, extracting the first zipfile, but hangs
on the next, supposedly because of 'permissions' but 'permissions'
were not really an issue.
I think it's the "is it a .zip or is it not a zip" issue.
Permissions were not an issue in the original task, which worked (but
had flawed data structure (my fault)).


> Maybe its the version of Python?
> Maybe I didn't read what you wrote to type into the shell properly?
> Maybe it's the spaces and dashes in the zipfile name? (the LMS does
> that (learning Management system)
Maybe it's the location of the unzip_twice.py script?


>> (the folder must already exist), it may do what you want. Make sure
>> you test it on a sample set of data, not the real thing.
yes, folder existed

>> Good luck and don't worry about asking dumb questions, the only dumb
>> question is "Was it you or your brother that was killed in the war?"
>
> Thanks, as you can tell, I need it!
Thanks again, I really do need it! (oh sorry, was that obvious? haha!)

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Gregory Lund
> Using Peter's code, if you create a plain text file called "unzip_twice.py"
> containing:
>
>
> import glob
> import os
> import sys
> import zipfile
>
> source_file = sys.argv[1]
> dest_folder = sys.argv[2]
>
> zipfile.ZipFile(source_file).extractall(dest_folder)
>
> inner_zips_pattern = os.path.join(dest_folder, "*.zip")
> for filename in glob.glob(inner_zips_pattern):
> inner_folder = filename[:-4]
> zipfile.ZipFile(filename).extractall(inner_folder)
>
Consider it done, i have a new .py file saved as 'unzip_twice.py'
pasted below:

import glob
import os
import sys
import zipfile

source_file = sys.argv[1]
dest_folder = sys.argv[2]

zipfile.ZipFile(source_file).extractall(dest_folder)

inner_zips_pattern = os.path.join(dest_folder, "*.zip")
for filename in glob.glob(inner_zips_pattern):
inner_folder = filename[:-4]
zipfile.ZipFile(filename).extractall(inner_folder)

>
> and then run it from the shell like this:
>
> python unzip_twice.py NAME-OF-ZIP-FILE NAME-OF-FOLDER-TO-EXTRACT-TO
>
In the IDLE 2.6.5 shell I typed (exactly what is in quotes, without
quotes of course):  "python unzip_twice.py 2012-09-18 Lab_2.zip
Student_Work_Sample_usecopy1"
where  '2012-09-18 Lab_2.zip' was/is the original zipfile and
'Student_Work_Sample_usecopy1' is the folder in which I want it all
housed/extracted

I got what is in quotes below:
"IDLE 2.6.5
>>> python unzip_twice.py 2012-09-18 Lab_2.zip Student_Work_Sample_usecopy1
SyntaxError: invalid syntax"
(unzip_twice) was the highlighted invalid syntax

I tried again:
In the IDLE 2.6.5 shell I typed (exactly what is in quotes, without
quotes of course):  "unzip_twice.py 2012-09-18 Lab_2.zip
Student_Work_Sample_usecopy1"
where  '2012-09-18 Lab_2.zip' was/is the original zipfile and
'Student_Work_Sample_usecopy1' is the folder in which it exists and I
want it all housed/extracted

I got what is in quotes below:
"IDLE 2.6.5
>>> unzip_twice.py 2012-09-18 Lab_2.zip Student_Work_Sample_usecopy1
SyntaxError: invalid syntax"
(2012) was the highlighted 'invalid syntax'

Maybe its the version of Python?
Maybe I didn't read what you wrote to type into the shell properly?
Maybe it's the spaces and dashes in the zipfile name? (the LMS does
that (learning Management system)


> (the folder must already exist), it may do what you want. Make sure
> you test it on a sample set of data, not the real thing.
I created copies of my original test data and folders:
'Student_Work_Sample_usecopy1' was the folder and w/in it: the actual
zipfile.

>
> I see that you're using Windows. I don't have Windows myself, but I
> think you'll probably have fewer problems with pathnames if you use
> forward slashes instead of backslashes. So:
>
> D:/D_Drive_Documents/Student_Work_Sample_use/Lab_2/aforker/
>
yes, Windows 7, but I didn't type out the slashes, they were coming in
via what the code was running.
I couldn't figure out where to ensure that they were forward (or
double backslashes).
> Good luck and don't worry about asking dumb questions, the only dumb
> question is "Was it you or your brother that was killed in the war?"

Thanks, as you can tell, I need it!
Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Steven D'Aprano

Further comments below:


On 20/09/12 01:10, Gregory Lund wrote:


Have you considered the simpler code I gave in

http://mail.python.org/pipermail/tutor/2012-August/090743.html

before prodding on?


Yes, I did consider it, but I didn't understand it enough to 'run with it'.
If it worked perfectly I still wouldn't of understood it, and it I
needed to tweak it, there would have been no way for me to figure out
what to do to make it fit my scenario.
While it may be 'simpler' for the experienced python coder, Not being
familiar with Python, it wasn't simpler for me, I could hardly read
any of it.
I even printed it out and saved, but I couldn't understand it and
didn't want to bother you with it any more (felt like an idiot, to be
honest) (you would of had to explain everything, as I didn't
understand hardly any of it)


That's what we're here for! Don't be shy about asking questions.

However, I have to say Peter was a bit optimistic in his assumptions about
your general level of expertise. He mixed Python code and (probably) Linux
shell commands and output, which probably didn't help.

Using Peter's code, if you create a plain text file called "unzip_twice.py" 
containing:


import glob
import os
import sys
import zipfile

source_file = sys.argv[1]
dest_folder = sys.argv[2]

zipfile.ZipFile(source_file).extractall(dest_folder)

inner_zips_pattern = os.path.join(dest_folder, "*.zip")
for filename in glob.glob(inner_zips_pattern):
inner_folder = filename[:-4]
zipfile.ZipFile(filename).extractall(inner_folder)


and then run it from the shell like this:

python unzip_twice.py NAME-OF-ZIP-FILE NAME-OF-FOLDER-TO-EXTRACT-TO

(the folder must already exist), it may do what you want. Make sure
you test it on a sample set of data, not the real thing.

You'll also need to make sure that you have write permission to the
folder, and read permission to the zip file. If you get Permission
Denied errors, check the permissions.

I see that you're using Windows. I don't have Windows myself, but I
think you'll probably have fewer problems with pathnames if you use
forward slashes instead of backslashes. So:

D:/D_Drive_Documents/Student_Work_Sample_use/Lab_2/aforker/

Good luck and don't worry about asking dumb questions, the only dumb
question is "Was it you or your brother that was killed in the war?"

:-)


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Steven D'Aprano

Hi Gregory,

On 20/09/12 01:10, Gregory Lund wrote:


Your lack of response in the previous thread

http://mail.python.org/pipermail/tutor/2012-August/090742.html

is not a big motivation to answer this one.


I didn't have any response to post, I got the basics to work using a
hint from a colleague in and was able to grade the assignment,

[...]

To an intermediate user, you most likely answered my questions, but I
didn't understand what you were saying/writing/typing.
It was full of code that I didn't (don't) understand (sys.argv, glob,
$ tree, etc.)


That's fine and nothing to be ashamed of. But don't be shy about asking
for explanations of what code does. If you're here to learn, you have
to ask questions!



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Gregory Lund
>
> Your lack of response in the previous thread
>
> http://mail.python.org/pipermail/tutor/2012-August/090742.html
>
> is not a big motivation to answer this one.

I didn't have any response to post, I got the basics to work using a
hint from a colleague in and was able to grade the assignment, however
it seems as though the way the files are NOW saved in the Learning
Management System are not as I prepared my testing situation.
I wasn't able to use what Peter provided because I didn't understand
it (again, rookie/neophyte).
To an intermediate user, you most likely answered my questions, but I
didn't understand what you were saying/writing/typing.
It was full of code that I didn't (don't) understand (sys.argv, glob,
$ tree, etc.)
I had to go with what I could understand or at least work with. I
tried studying up on sys.argv, glob, etc. and got lost in the details)
Again, a colleague was able to help enough to get it to work using my
simplified code. (or at least code I could read).

>>
>> originalzip.zip
>> --originalfolder
>>   --folder1 (w/ two zip folders)
>> --internalzip1_a.zip
>> --internalfolder1_a
>>   --files
>>   --folders
>> --internalzip1_b.zip
>> --internalfolder1_b
>>   --files
>>   --folders
>>   --folder2 (w/1 zip folder)
>> --internalzip2.zip
>> --internalfolder2
>>   --files
>>   --folders
>>   --etc

I attempted to replicate Peter's 'Folder Structure' diagram above, he
did a better job 'drawing it' but the one above is my data situation.
Using my sample data, this is what the folder structure resembles below.

|__ Student_Work_Sample_use (a folder I create before running the
script, the original zip is placed in this folder before running the
script)
  |__originalzip.zip (entire class all zipped up into one) (THIS
NEEDS TO BE UNZIPPED IN THE CODE)
  |__Lab_2 (a folder that is the contents of originalzip.zip unzipped)
  |__aforker (unzipped folder that comes in Lab_2)
  |__aforker_lab_2.zip (student username 'aforker''s' lab)
(THIS NEEDS TO BE UNZIPPED IN THE CODE)
  |__aforker_lab_writeup.docx (a docx file that may or may
not come in with the lab submission (it may or may not be zipped, if
it's not zipped, no need to deal with it)
  |__aforker_lab_2 (a folder that is the contents of
aforker_lab_2.zip, unzipped)
   |__ datafolder (unzipped folders within aforkers
lab folder) (no need to worry about, just here for reference)
   |__ mapsfolder (unzipped folders within aforkers
lab folder) (no need to worry about, just here for reference)
  |__awilliams (unzipped folder that comes in Lab_2)
  |__awilliams_lab_2.zip (student username 'awilliams''s'
lab) (THIS NEEDS TO BE UNZIPPED IN THE CODE)
  |__awilliams_lab_2 (a folder that is the contents of
awilliams_lab_2.zip, unzipped)
   |__ datafolder (unzipped folders within awilliams
lab folder) (no need to worry about, just here for reference)
   |__ mapsfolder (unzipped folders within awilliams
lab folder) (no need to worry about, just here for reference)
  |__awilliams_lab_2_RESUB.zip (student username
'awilliams''s' lab) (THIS NEEDS TO BE UNZIPPED IN THE CODE)
  |__awilliams_lab_2_RESUB (a folder that is the contents
of awilliams_lab_2_RESUB.zip, unzipped)
   |__ datafolder (unzipped folders within awilliams
lab folder) (no need to worry about, just here for reference)
   |__ mapsfolder (unzipped folders within awilliams
lab folder) (no need to worry about, just here for reference)
  |__jsmith (unzipped folder that comes in Lab_2)
  |__jsmith_lab_2.zip (student username 'jsmith''s' lab)
(THIS NEEDS TO BE UNZIPPED IN THE CODE)
  |__jsmith_lab_2 (a folder that is the contents of
jsmith_lab_2.zip, unzipped)
   |__ datafolder (unzipped folders within jsmith lab
folder) (no need to worry about, just here for reference)
   |__ mapsfolder (unzipped folders within jsmith lab
folder) (no need to worry about, just here for reference)
  |__ etc. etc. etc. up to 40 students.

>>
>> My goal is to:
>> a) Unzip the 'originalzip.zip'
>> b) go to the 'originalfolder' (the unzipped version of the
>> originalzip.zip) c) go into the first folder (folder1) in the original
>> folder and unzip any and all zipfiles within it
>> d) go to the second folder (folder2) in the original folder and unzip
>> any and all zipfiles within it
>> e) continue until all folders within originalfolders have been checked
>> for internalzips
>>
>>
>> I have some code that works to extract the 'originalzip.zip', to an
>> 'originalfolder' but it won't go to the folders (folder1, folder2,
>> etc.) to unzip the zipfiles within them.
>> It gets hung up on access to the first student folder and won't unzip it.
>
> Hm, I would have expeced an exception. Perhaps you s

Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Peter Otten
Gregory Lund wrote:

> I teach at a college and I'm trying to use Python (2.6 because I'm
> running my tool in ArcGIS) to unzip a .zip file that contains one
> folder full of student folders, each with 1 or more submissions
> (zipfiles) that represent student submissions to weekly lab
> assignments.

Your lack of response in the previous thread

http://mail.python.org/pipermail/tutor/2012-August/090742.html

is not a big motivation to answer this one.
 
> It all starts with an originalzip.zip (for example) that has a single
> folder (originalfolder)
> Within the 'originalfolder' folder there are anywhere from 1 - 40
> folders (that are not zipped). (these are the students userid folders)
> Within each of the (1-40) student userid folders is anywhere from 1-10
> zipfiles and perhaps a .pdf or .docx (if for example they submitted
> more than one revision of the assignment, there are more than 1)
> 
> Folder Structure
> 
> originalzip.zip
> --originalfolder
>   --folder1 (w/ two zip folders)
> --internalzip1_a.zip
> --internalfolder1_a
>   --files
>   --folders
> --internalzip1_b.zip
> --internalfolder1_b
>   --files
>   --folders
>   --folder2 (w/1 zip folder)
> --internalzip2.zip
> --internalfolder2
>   --files
>   --folders
>   --etc
> 
> My goal is to:
> a) Unzip the 'originalzip.zip'
> b) go to the 'originalfolder' (the unzipped version of the
> originalzip.zip) c) go into the first folder (folder1) in the original
> folder and unzip any and all zipfiles within it
> d) go to the second folder (folder2) in the original folder and unzip
> any and all zipfiles within it
> e) continue until all folders within originalfolders have been checked
> for internalzips
> 
> 
> ### Note, I am a beginner both with this tutor environment and in python.
> I apologize in advance if my code below is 'not up to par' but I am
> trying to keep it simple in nature and use ample comments to keep
> track of what I am doing. I also don't know if I should post sample
> data (zipfile of a folder of folders with zipfiles), and if so, where?
> 
> I have some code that works to extract the 'originalzip.zip', to an
> 'originalfolder' but it won't go to the folders (folder1, folder2,
> etc.) to unzip the zipfiles within them.
> It gets hung up on access to the first student folder and won't unzip it.

Hm, I would have expeced an exception. Perhaps you should omit the ArcGIS 
integration until everything else works.

> I think it's a simple fix, but I've been messing with it for quite a
> while and can't figure it out.
> 
> Code below:
> 
> #1 Required imports.

Excessive comments impair readability. Comments stating the obvious are 
particularly bad.

> import os, os.path, zipfile, arcpy
> 
> #2 I'm Utilizing 'GetParameterAsText' so that this code can be run as
> a tool in ArcGIS
> 
> #2.1 in_zip is a variable for "What zipfile (LAB) do you want to extract?"
> in_Zip = arcpy.GetParameterAsText(0)
> cZ = in_Zip

Why two names for one value?
 
> #2.2 outDir is a variable for "What is your output Directory?"
> outDir = os.getcwd()
> 
> #3 Extracting the initial zipfolder:
> #3.1 Opening the original zipfile
> z = zipfile.ZipFile(cZ)
> 
> #4 Extracting the cZ (original zipfile)into the output directory.
> z.extractall(outDir)
> 
> #5 Getting a list of contents of the original zipfile
> zipContents = z.namelist()
> 
> #6 Unzipping the Inner Zips:
> 
> #6.1 Looping through the items that were in the original zipfile, and
> now in a folder...
> #   ...For each item in the zipContents
> for item in zipContents:

You make no attempt to filter out the contents that are not zipfiles. That 
will cause an exception further down where you try to unzip.
 
> #6.2 Get the location (note the location is the 'outDir' plus what
> namelist() gave me)
> #(have never used 'os.sep', had to look it up, when someone suggested
> #it)
> itemLoc = outDir + os.sep + item

The standard way (which is also more robust) is to use os.path.join():

  itemLoc = os.path.join(outDir, item)
 
> #6.3 Opens the first (2nd, 3rd, etc files) of the internal zip file
> #(*.zip)
> z = zipfile.ZipFile(itemLoc)
> 
> #6.4 Extract all files in *.zip in the same folder as the zipfile
> z.extractall(os.path.split(itemLoc)[0])

The zip files's contents will probably end up in the same folder as the 
zipfile. Is that what you want?
> 
> #6.5 determining the list of items in each students' folder
> student_lab = z.namelist()

Unused variable alert.

> 
> #7 THE END.
> 
> Thank you for any and all suggestions/ fixes, constructive criticism
> and assistance with my beginner code!

Have you considered the simpler code I gave in 

http://mail.python.org/pipermail/tutor/2012-August/090743.html

before prodding on?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor