Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Generating directories on the fly

Holiverh's izHawt Python....

Generating directories on the fly

Postby Holiverh » Tue Nov 24, 2009 10:50 am

Hello ya'll, i have yet another problem. :D

Basically i have ZIP file for which i use a zipfile.ZipFile() object to represent. I'm trying to use namelist() to generate the file paths to where i will "copy" the files to. But the problem is i've got directories inside the ZIP aswell so i'd need to create them as well, but i don't know how to distinguish between what's meant to be a file and whats meant to be a directory.

The below generates and prints all the valid file paths.
Code: Select all
zip_path=os.path.join("data","tmp","TEMP20091123sourceforge.net_projects_shellfire_files_shell_fire_v0_003_src.zip_download")
zip = zipfile.ZipFile(zip_path, "r", allowZip64=False)
self.__extractZip(os.getcwd(), zip)

def __extractZip(self, dir, zobj):
      new_path = dir
      
      for path in zobj.namelist():
         for chunk in path.split("/"):
            if chunk != "":
               new_path = os.path.join(new_path, chunk)
         
         print new_path
         new_path = dir
      return None


Example of what it returns
Code: Select all
/home/oliver/Desktop/PackageUpdater/pgu
/home/oliver/Desktop/PackageUpdater/pgu/algo.py
/home/oliver/Desktop/PackageUpdater/pgu/ani.py
/home/oliver/Desktop/PackageUpdater/pgu/engine.py
/home/oliver/Desktop/PackageUpdater/pgu/fonts.py
/home/oliver/Desktop/PackageUpdater/pgu/gui
/home/oliver/Desktop/PackageUpdater/pgu/gui/app.py

/home/oliver/Desktop/PackageUpdater/changelog
/home/oliver/Desktop/PackageUpdater/controller.py
/home/oliver/Desktop/PackageUpdater/defs.py
/home/oliver/Desktop/PackageUpdater/license
/home/oliver/Desktop/PackageUpdater/main.py
/home/oliver/Desktop/PackageUpdater/player.py
/home/oliver/Desktop/PackageUpdater/readme
/home/oliver/Desktop/PackageUpdater/terrain
Now a really crude way of detecting what is a directory and what isn't would be checking to see if the last chunk of the path contained a ".", but this raises two problems:
  • Some files don't have an extension
  • Some directories have periods in them
So i need a foolproof way of doing this and i really can't think of one. Any ideas?
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: Generating directories on the fly

Postby Holiverh » Wed Nov 25, 2009 4:35 am

Bleh! As usual i worked out the answer to my own problem again.

When i was doing the whole:
Code: Select all
if chunk != "":
   new_path = os.path.join(new_path, chunk)
I was completely missing the fact that the "" is the string that identifies the path as a directory. Silly me. :lol:

Finished fully functional code
Code: Select all
def __extractZip(self, dir, zobj):
      new_path = dir
      isfile = None
      
      for path in zobj.namelist():
         for chunk in path.split("/"):
            if chunk == "":
               isfile = False
            else:
               isfile = True
               new_path = os.path.join(new_path, chunk)
         
         ## Creates a directory or file based on the content within
            # the ZipFile
         if isfile:
            fobj = open(new_path, "ab")
            
            for line in zobj.readlines(path):
               fobj.write(line)
            
            fobj.close()
            
         if not isfile:
            if not os.path.isdir(new_path):
               os.makedirs(new_path)
         
         ## Reset the new_path so it can be compiled again
         new_path = dir
         
      zobj.close()
      return None


:mrgreen:
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK


Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred