使用 Python 2.7.5 将文件夹中的所有压缩文件解压缩到同一文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31346790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unzip all zipped files in a folder to that same folder using Python 2.7.5
提问by tpdance
I would like to write a simple script to iterate through all the files in a folder and unzip those that are zipped (.zip) to that same folder. For this project, I have a folder with nearly 100 zipped .las files and I'm hoping for an easy way to batch unzip them. I tried with following script
我想编写一个简单的脚本来遍历文件夹中的所有文件,并将那些已压缩 (.zip) 的文件解压缩到同一文件夹中。对于这个项目,我有一个包含近 100 个压缩 .las 文件的文件夹,我希望有一种简单的方法来批量解压缩它们。我尝试使用以下脚本
import os, zipfile
folder = 'D:/GISData/LiDAR/SomeFolder'
extension = ".zip"
for item in os.listdir(folder):
if item.endswith(extension):
zipfile.ZipFile.extract(item)
However, when I run the script, I get the following error:
但是,当我运行脚本时,出现以下错误:
Traceback (most recent call last):
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in <module>
extract = zipfile.ZipFile.extract(item)
TypeError: unbound method extract() must be called with ZipFile instance as first argument (got str instance instead)
I am using the python 2.7.5 interpreter. I looked at the documentation for the zipfile module (https://docs.python.org/2/library/zipfile.html#module-zipfile) and I would like to understand what I'm doing incorrectly.
我正在使用 python 2.7.5 解释器。我查看了 zipfile 模块的文档(https://docs.python.org/2/library/zipfile.html#module-zipfile),我想了解我做错了什么。
I guess in my mind, the process would go something like this:
我想在我看来,这个过程会是这样的:
- Get folder name
- Loop through folder and find zip files
- Extract zip files to folder
- 获取文件夹名称
- 遍历文件夹并找到 zip 文件
- 将 zip 文件解压缩到文件夹
Thanks Marcus, however, when implementing the suggestion, I get another error:
谢谢马库斯,但是,在实施建议时,我遇到了另一个错误:
Traceback (most recent call last):
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 12, in <module>
zipfile.ZipFile(item).extract()
File "C:\Python27\ArcGIS10.2\lib\zipfile.py", line 752, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'JeffCity_0752.las.zip'
When I use print statements, I can see that the files are in there. For example:
当我使用打印语句时,我可以看到文件在那里。例如:
for item in os.listdir(folder):
if item.endswith(extension):
print os.path.abspath(item)
filename = os.path.basename(item)
print filename
yields:
产量:
D:\GISData\Tools\MO_Tools\JeffCity_0752.las.zip
JeffCity_0752.las.zip
D:\GISData\Tools\MO_Tools\JeffCity_0753.las.zip
JeffCity_0753.las.zip
As I understand the documentation,
据我了解文档,
zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
Open a ZIP file, where file can be either a path to a file (a string) or a file-like object
打开一个 ZIP 文件,其中 file 可以是文件的路径(字符串)或类似文件的对象
It appears to me like everything is present and accounted for. I just don't understand what I'm doing wrong.
在我看来,一切都存在并已考虑在内。我只是不明白我做错了什么。
Any suggestions?
有什么建议?
Thank You
谢谢你
采纳答案by tpdance
Below is the code that worked for me:
以下是对我有用的代码:
import os, zipfile
dir_name = 'C:\SomeDirectory'
extension = ".zip"
os.chdir(dir_name) # change directory from working dir to dir with files
for item in os.listdir(dir_name): # loop through items in dir
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(dir_name) # extract file to dir
zip_ref.close() # close file
os.remove(file_name) # delete zipped file
Looking back at the code I had amended, the directory was getting confused with the directory of the script.
回顾我修改过的代码,该目录与脚本目录混淆了。
The following also works while not ruining the working directory. First remove the line
以下也可以在不破坏工作目录的情况下工作。首先删除该行
os.chdir(dir_name) # change directory from working dir to dir with files
Then assign file_name as
然后将 file_name 分配为
file_name = dir_name + "/" + item
回答by Marcus Müller
You need to construct a ZipFile
object with the filename, and thenextract it:
您需要ZipFile
使用文件名构造一个对象,然后提取它:
zipfile.ZipFile.extract(item)
is wrong.
是错的。
zipfile.ZipFile(item).extractall()
will extract all files from the zip file with the name contained in item
.
将从 zip 文件中提取名称包含在 .zip 文件中的所有文件item
。
I think you should more closely read the documentation to zipfile
:) but you're on the right track!
我认为你应该更仔细地阅读文档zipfile
:) 但你走在正确的轨道上!
回答by 0Nicholas
The accepted answer works great!
接受的答案效果很好!
Just to extend the idea to unzip all the files with .zip extension within all the sub-directories inside a directory the following code seems to work well:
只是为了扩展这个想法以解压缩目录内所有子目录中所有带有 .zip 扩展名的文件,以下代码似乎运行良好:
import os
import zipfile
for path, dir_list, file_list in os.walk(dir_path):
for file_name in file_list:
if file_name.endswith(".zip"):
abs_file_path = os.path.join(path, file_name)
# The following three lines of code are only useful if
# a. the zip file is to unzipped in it's parent folder and
# b. inside the folder of the same name as the file
parent_path = os.path.split(abs_file_path)[0]
output_folder_name = os.path.splitext(abs_file_path)[0]
output_path = os.path.join(parent_path, output_folder_name)
zip_obj = zipfile.ZipFile(abs_file_path, 'r')
zip_obj.extractall(output_path)
zip_obj.close()
回答by Bondify
I think this is shorter and worked fine for me. First import the modules required:
我认为这更短,对我来说效果很好。首先导入所需的模块:
import zipfile, os
Then, I define the working directory:
然后,我定义工作目录:
working_directory = 'my_directory'
os.chdir(working_directory)
After that you can use a combination of the os
and zipfile
to get where you want:
之后,你可以使用的一个组合os
,并zipfile
以得到你想要的:
for file in os.listdir(working_directory): # get the list of files
if zipfile.is_zipfile(file): # if it is a zipfile, extract it
with zipfile.ZipFile(file) as item: # treat the file as a zip
item.extractall() # extract it in the working directory