'How do I determine whether an item in python ZipFile is dir or file?

This is my current system, which just looks to see whether there is a period in the path... but that doesn't work in all cases

    with ZipFile(zipdata, 'r') as zipf: #Open the .zip BytesIO in Memory
        
        mod_items = zipf.namelist()     #Get the name of literally everything in the zip
        
        for folder in mod_folders:

            mod_items.insert(0, folder) 
            #Put the folder at the start of the list
            #This makes sure that the folder gets created 
            
            #Search through every file in the .zip for each folder indicated in the save
            for item in mod_items: 

                if folder in item:  #If the item's path indicates that it is a member of the folder
                    
                    itempath = item #Make a copy of the 
                    
                    if not itempath.startswith(folder):
                        itempath = path_clear(itempath, folder)
                        
                    if not 'GameData/' in itempath:
                        itempath = 'GameData/' + itempath
                        
                    path = mod_destination_folder + '/' + itempath
                    path = path_clear(path)
                    dot_count = path.count('.')
                    
                    if dot_count: #Is a normal file, theoretically
                        
                        itemdata = zipf.read(item)
                        
                        try:
                            with open(path, 'wb') as file:
                                file.write(itemdata)
                                
                        except FileNotFoundError as f:
                            folder_path = path[:path.rfind('/')]
                            makedirs(folder_path)
                            with open(path, 'wb') as file:
                                file.write(itemdata)

                    else: #Is a folder
                        try: makedirs(path)
                        except: pass

All I need is some sort of way to determine whether: A) Whether the folder is one of the folders desired by the user B) For each item in that folder is a directory or a file



Solution 1:[1]

Use ZipInfo.is_dir:

zipf.getinfo(name).is_dir()

You can also use infolist instead of namelist to get ZipInfo objects directly instead of names.

Alternatively, check the name - directories will end with /. (This is the same check that is_dir performs.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1