Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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库提取文件_Python_Zip_Zipfile - Fatal编程技术网

如何在更改文件名的同时使用python zipfile库提取文件

如何在更改文件名的同时使用python zipfile库提取文件,python,zip,zipfile,Python,Zip,Zipfile,这是由pathfile问题引起的不幸的是,在我的案例中似乎不是这样 我有一个zipfile,我正试图用python提取它。zipfile似乎是在windows上创建的。我必须从zipfile中提取文件的代码如下: def unzip_file(zipfile_path): z = zipfile.ZipFile(zipfile_path) # get pathname without extension directory = os.path.splitext(zipfi

这是由pathfile问题引起的不幸的是,在我的案例中似乎不是这样

我有一个zipfile,我正试图用python提取它。zipfile似乎是在windows上创建的。我必须从zipfile中提取文件的代码如下:

def unzip_file(zipfile_path):
    z = zipfile.ZipFile(zipfile_path)
    # get pathname without extension
    directory = os.path.splitext(zipfile_path)[0]
    print directory
    if not os.path.exists(directory):
        os.makedirs(directory)
    #this line doesn't work. tries to extract "Foobar\\baz.quux" to directory and complains that the directory doesn't exist
    # z.extractall(directory)
    for name in z.namelist():
        # actual dirname we want is this
        # (dirname, filename) = os.path.split(name)
        # I've tried to be cross-platform, (see above) but aparently zipfiles save filenames as
        # Foobar\filename.log so I need this for cygwin
        dir_and_filename = name.split('\\')
        if len(dir_and_filename) >1:
            dirname = dir_and_filename[0:-1]
            filename = dir_and_filename[-1]
        else:
            dirname = ['']
            filename = dir_and_filename[0]

        out_dir = os.path.join(directory, *dirname)
        print "Decompressing " + name + " on " + out_dir
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        z.extract(name, out_dir)
    return directory
虽然这看起来过于复杂,但这是为了尝试解决我发现的一些bug。zipfile的一个成员是Foobar\\filename.log。在尝试提取时,它会抱怨目录不存在。我需要一种方法来使用这样的方法:

zipfile.extract_to(member_name, directory_name, file_name_to_write)
其中,member name是本例中要读取的成员的名称Foobar\\filename.log,directory_name是我们要写入的目录的名称,file_name_to_write是我们要写入的文件的名称。在本例中,它将是filename.log。这似乎没有得到支持。对于如何获得跨平台实现来提取这种具有嵌套表达式的zip存档,有人有其他想法吗

根据zipfile,我所拥有的zipfile可能不符合zipfile规范,它说:

所有的斜杠 必须是正斜杠“/”而不是 向后斜杠“\”与Amiga兼容 和UNIX文件系统等

在4.4.17中
我如何解决这个问题?

我通过简单的解压解决了这个问题。由于zipfile格式不正确,解压命令返回一个退出代码时,我们需要检查退出代码是否为0或1,给出的消息类似于警告:zipfile似乎包含反斜杠作为路径分隔符


如果问题是任何斜杠字符的方向,在使用它之前,你不能用正斜杠替换路径文件中的任何反斜杠吗?@martineau这就是我想要做的,但是有两个不同的概念,成员名称的概念,它说明数据在zipfile中的位置,本例中的foobar\\filename.log是我想写出的文件路径的概念。我找不到一个方法可以同时指定,我可以使用zipfile.extractA unzip_filer'whatever\testzip.zip'调用您的代码在whatever\testzip\Foobar上打印解压Foobar\filename.log并尝试z.extractor'Foobar\filename.log'来指定写入文件名的成员名和写入文件名的目录,r'which\testzip\Foobar',这可能意味着它将尝试创建后一个命名子目录。在我的Windows系统上,ZipFile被操作系统自动视为子目录,操作系统可能会认为您正在尝试创建一个已经存在的子目录,因为在同一上层目录的zip文件中,逻辑上有一个同名的子目录。尝试将其作为一个实验覆盖,看看会发生什么。
#!/bin/bash
unzip $1 -d $2
exit_code=$?
# we catch exit_codes < 2 as the zipfiles are malformed
if [ $exit_code -lt 2 ]
then exit 0
else exit $exit_code
fi