使用 Python 2.7.5 将文件夹中的所有压缩文件解压缩到同一文件夹 [英] Unzip all zipped files in a folder to that same folder using Python 2.7.5
问题描述
我想编写一个简单的脚本来遍历文件夹中的所有文件,并将那些已压缩 (.zip) 的文件解压缩到同一文件夹中.对于这个项目,我有一个包含近 100 个压缩 .las 文件的文件夹,我希望有一种简单的方法来批量解压缩它们.我尝试使用以下脚本
import os, zipfile文件夹 = 'D:/GISData/LiDAR/SomeFolder'扩展名 = ".zip"对于 os.listdir(folder) 中的项目:如果 item.endswith(extension):zipfile.ZipFile.extract(item)
但是,当我运行脚本时,出现以下错误:
回溯(最近一次调用最后一次):文件D:/GISData/Tools/MO_Tools/BatchUnzip.py",第 10 行,在 <module> 中提取 = zipfile.ZipFile.extract(item)类型错误:必须使用 ZipFile 实例作为第一个参数调用未绑定的方法 extract()(改为使用 str 实例)
我使用的是 python 2.7.5 解释器.我查看了 zipfile 模块的文档(https://docs.python.org/2/library/zipfile.html#module-zipfile),我想了解我做错了什么.
在我看来,这个过程应该是这样的:
- 获取文件夹名称
- 遍历文件夹并找到 zip 文件
- 将 zip 文件解压到文件夹
谢谢 Marcus,但是,在实施建议时,我又遇到了另一个错误:
回溯(最近一次调用最后一次):文件D:/GISData/Tools/MO_Tools/BatchUnzip.py",第 12 行,在 <module> 中zipfile.ZipFile(item).extract()文件C:\Python27\ArcGIS10.2\lib\zipfile.py",第 752 行,在 __init__ 中self.fp = 打开(文件,modeDict[mode])IOError: [Errno 2] 没有这样的文件或目录:'JeffCity_0752.las.zip'
当我使用打印语句时,我可以看到文件在那里.例如:
用于 os.listdir(文件夹)中的项目:如果 item.endswith(extension):打印 os.path.abspath(item)文件名 = os.path.basename(item)打印文件名
产量:
D:\GISData\Tools\MO_Tools\JeffCity_0752.las.zipJeffCity_0752.las.zipD:\GISData\Tools\MO_Tools\JeffCity_0753.las.zipJeffCity_0753.las.zip
据我了解文档,
zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
<块引用>
打开一个 ZIP 文件,其中 file 可以是文件的路径(字符串)或类似文件的对象
在我看来,一切都已存在并已考虑在内.我只是不明白我做错了什么.
有什么建议吗?
谢谢
以下是对我有用的代码:
import os, zipfiledir_name = 'C:\\SomeDirectory'扩展名 = ".zip"os.chdir(dir_name) # 将目录从工作目录更改为带有文件的目录for item in os.listdir(dir_name): # 遍历目录中的项目if item.endswith(extension): # 检查.zip"扩展名file_name = os.path.abspath(item) # 获取文件的完整路径zip_ref = zipfile.ZipFile(file_name) # 创建 zipfile 对象zip_ref.extractall(dir_name) # 提取文件到目录zip_ref.close() # 关闭文件os.remove(file_name) # 删除压缩文件
回顾我修改过的代码,目录和脚本的目录混淆了.
以下也有效,同时不会破坏工作目录.先去掉一行
os.chdir(dir_name) # 将目录从工作目录更改为带有文件的目录
然后将 file_name 赋值为
file_name = dir_name + "/" + item
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
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.
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
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
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
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 = dir_name + "/" + item
这篇关于使用 Python 2.7.5 将文件夹中的所有压缩文件解压缩到同一文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!