Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中打开Zip文件中的文件夹_Python_Python 2.7_Zip_Directory - Fatal编程技术网

在Python中打开Zip文件中的文件夹

在Python中打开Zip文件中的文件夹,python,python-2.7,zip,directory,Python,Python 2.7,Zip,Directory,是否可以在不解压缩文件的情况下打开zip文件中的文件夹并查看其内容的名称? 这就是我目前所拥有的 zipped_files = zip.namelist() print zipped_files for folder in zipped_files: for files in folder: #I'm not sure if this works 有人知道怎么做吗?或者我必须提取内容吗?下面是一个我躺在那里的拉链的例子 >>> fr

是否可以在不解压缩文件的情况下打开zip文件中的文件夹并查看其内容的名称? 这就是我目前所拥有的

    zipped_files = zip.namelist()
    print zipped_files
    for folder in zipped_files:
        for files in folder:   #I'm not sure if this works

有人知道怎么做吗?或者我必须提取内容吗?

下面是一个我躺在那里的拉链的例子

>>> from zipfile import ZipFile
>>> zip = ZipFile('WPD.zip')
>>> zip.namelist()
['PortableDevices/', 'PortableDevices/PortableDevice.cs', 'PortableDevices/PortableDeviceCollection.cs', 'PortableDevices/PortableDevices.csproj', 'PortableDevices/Program.cs', 'PortableDevices/Properties/', 'PortableDevices/Properties/AssemblyInfo.cs', 'WPD.sln']
zip是平面存储的,每个“文件名”都有自己的内置路径

编辑:这里有一个方法,我用它快速地从文件列表中创建了一种结构

def deflatten(names):
    names.sort(key=lambda name:len(name.split('/')))
    deflattened = []
    while len(names) > 0:
        name = names[0]
        if name[-1] == '/':
            subnames = [subname[len(name):] for subname in names if subname.startswith(name) and subname != name]
            for subname in subnames:
                names.remove(name+subname)
            deflattened.append((name, deflatten(subnames)))
        else:
            deflattened.append(name)
        names.remove(name)
    return deflattened


>>> deflatten(zip.namelist())
['WPD.sln', ('PortableDevices/', ['PortableDevice.cs', 'PortableDeviceCollection.cs', 'PortableDevices.csproj', 'Program.cs', ('Properties/', ['AssemblyInfo.cs'])])]

这是一个我躺在那里的拉链的例子

>>> from zipfile import ZipFile
>>> zip = ZipFile('WPD.zip')
>>> zip.namelist()
['PortableDevices/', 'PortableDevices/PortableDevice.cs', 'PortableDevices/PortableDeviceCollection.cs', 'PortableDevices/PortableDevices.csproj', 'PortableDevices/Program.cs', 'PortableDevices/Properties/', 'PortableDevices/Properties/AssemblyInfo.cs', 'WPD.sln']
zip是平面存储的,每个“文件名”都有自己的内置路径

编辑:这里有一个方法,我用它快速地从文件列表中创建了一种结构

def deflatten(names):
    names.sort(key=lambda name:len(name.split('/')))
    deflattened = []
    while len(names) > 0:
        name = names[0]
        if name[-1] == '/':
            subnames = [subname[len(name):] for subname in names if subname.startswith(name) and subname != name]
            for subname in subnames:
                names.remove(name+subname)
            deflattened.append((name, deflatten(subnames)))
        else:
            deflattened.append(name)
        names.remove(name)
    return deflattened


>>> deflatten(zip.namelist())
['WPD.sln', ('PortableDevices/', ['PortableDevice.cs', 'PortableDeviceCollection.cs', 'PortableDevices.csproj', 'Program.cs', ('Properties/', ['AssemblyInfo.cs'])])]

你读过了吗?最好执行代码,自己去发现。如果它有效,你很幸运,如果没有,你会学到一些新东西。你读过吗?最好执行代码,自己去发现。如果成功,你就很幸运,如果失败,你就会学到新东西。