[Tutor] Zipfile and File manipulation questions.

Chris Hengge pyro9219 at gmail.com
Sun Oct 15 10:38:04 CEST 2006

I must have not been clear.. I have a zip file with folders inside..
When I extract it.. I dont want the folder structure, just the files..

using split doesn't seem to help in this situation.. unless I'm not putting
the results in the right spot..

Thanks again though.

On 10/15/06, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> On 10/14/06, Chris Hengge <pyro9219 at gmail.com> wrote:
> >
> > Guess nobody has had a chance to point me in the write direction on this
> > problem yet?
> >
> > Thats ok, I've gotten a ton of other code written, and I'll come back to
> > this tricky part later.
> >
> > This particular project I've been working on to automate some of my job
> > at work has been an excellent learning experience. :D
> >
> > On 10/14/06, Chris Hengge < pyro9219 at gmail.com> wrote:
> > >
> > > I was using afile.split("/"), but I'm not sure how to impliment it...
> >
> >
> I think kent johnson gave you a  solution to this...
> was it not acceptable?
> Another alternate route you can take is os.path.split(path)[-1]
> this will give you the filename no matter what system you're on.
> it should work for '\\' and '/'.
> Are you making a script to automatically unzip something?
> ( I confess, I didn't read the other e-mails before this one.)
> If you are, and the problem you're having is with writing files to
> subdirectories,
> you'd want to do something like this:
> check if directory you want exists
> if it does, change into it.
> if it doesn't, create it and then change into it.
> recursively do this until you get to where you want to create the file...
> then create the file you want.
> in python, this would look like:
> import os
> path = 'directory/subdir/filename.txt' #this is the path you use from your
> zipfile or w/e
> path = os.path.split(path) #this will make a list ['directory','subdir','
> filename.txt']
> for item in path[:-1]: #everything but the last item (which is the
> filename)
>     if item not in os.listdir(): #directory doesn't exist
>         os.mkdir(item)#so we'll create it
>     os.chdir(item)#it doesn't matter to us if the directory exists before,
> we change into it either way.
> f = file(path[-1])#create the file you wanted
> f.write("Hello, this is the file!!!!")#change this to write the file info
> from out of the zip.
> f.close()#close file :)
> #now let's go back to the parent directory so we don't get lost later on
> :)
> for x in path[:-1]:#for every directory that we've changed into,
>     os.chdir('..') #go back to parent directory.
> I don't have a python interp installed on this computer, so there may be
> errors,
> and if so, I apologize in advance, but I can't see any reason why this
> wouldn't work.
> I just realized os.path doesn't do what I thought it does.  It only splits
> the end.
> so I guess my solution above would work, but you'd have to split it a
> different way :)
> I hope that helps,
> -Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061015/d080a04c/attachment.htm 

More information about the Tutor mailing list