如何将 zip 文件移动到新目标,然后在 python 3 中打开它 [英] How to move a zip file to a new destination and then open it in python 3

查看:43
本文介绍了如何将 zip 文件移动到新目标,然后在 python 3 中打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 zip 文件移动到新的目的地,然后在 python 3 中打开它.我编写了以下代码,但它似乎不适用于 zip 文件.

How to move a zip file to a new destination and then open it in python 3. I have made following code, but it seems it does not work for zip file.

import os

source = "C:/Users/sa/Desktop/Pic_ - Im.zip"

destination = "C:/Users/sa/Pictures/pic"

os.rename(source, destination)

推荐答案

这会将 zip 从一个位置移动到另一个位置,然后将其内容提取到您选择的目录(other_dir,在此实例)

This will move the zip from one location to another and then extract its contents to a directory of your choosing (other_dir, in this instance)

import shutil
import zipfile
from contextlib import closing

def _unzip(archive, destination):
    with closing(zipfile.ZipFile(archive, 'r')) as zip_file:
        zip_file.extractall(destination)

SOURCE = "C:/Users/sa/Desktop/Pic_ - Im.zip"
DESTINATION = "C:/Users/sa/Pictures/pic"

shutil.move(SOURCE, DESTINATION)

_unzip(DESTINATION, other_dir)

这篇关于如何将 zip 文件移动到新目标,然后在 python 3 中打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆