How to move a zip file to a new destination and then open it in python 3

白昼怎懂夜的黑 提交于 2021-02-11 08:49:54

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/42750684/how-to-move-a-zip-file-to-a-new-destination-and-then-open-it-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!