Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何确定python ZipFile中的项是dir还是file?_Python_Python 3.x_File_Zip_Zipfile - Fatal编程技术网

如何确定python ZipFile中的项是dir还是file?

如何确定python ZipFile中的项是dir还是file?,python,python-3.x,file,zip,zipfile,Python,Python 3.x,File,Zip,Zipfile,这是我当前的系统,它只是查看路径中是否有一个周期。。。但这并不适用于所有情况 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_folde

这是我当前的系统,它只是查看路径中是否有一个周期。。。但这并不适用于所有情况

    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
我需要的只是某种方式来确定: A) 文件夹是否为用户所需的文件夹之一 B) 对于该文件夹中的每个项目都是目录或文件,请使用:

您还可以使用
infolist
而不是
namelist
直接获取ZipInfo对象,而不是名称


或者,检查名称-目录将以
/
结尾。(这与
is_dir
执行的检查相同。)

So
用于zipf.namelist()中的名称:如果zipf.getinfo(name).is_dir()
?@artificialnintelligence:或使用
infolist
。我可能会选择
infolist
。然后我该如何处理每个ZipInfo对象?另外,我正在努力降低内存使用率,因为一台机器上会同时运行多达16个这样的实例,所以它们是大的吗?
zipf.getinfo(name).is_dir()