问题
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