简体   繁体   中英

Error when Unzipping with Pyside Qtgui

When I run my program, I get the following error and am not sure on how to correct it. Can someone help with explaining what this error is and how to correct it? Newb here so details are appreciated. Thanks for your time in advance!

Code:

#!/usr/bin/python
import zipfile
from PySide import QtGui
import re

#Select file to extract
app = QtGui.QApplication([])
dialog = QtGui.QFileDialog()
dialog.setFileMode(QtGui.QFileDialog.AnyFile)
if (dialog.exec()):
     fileName = dialog.selectedFiles()


#Select Directory to extract to
dialog = QtGui.QFileDialog()
dialog.setFileMode(QtGui.QFileDialog.Directory)
dialog.setOption(QtGui.QFileDialog.ShowDirsOnly)
if (dialog.exec()):
     dirName = dialog.selectedFiles()

print("Extracting.....")
zFile= zipfile.ZipFile(fileName)
zFile.extractall(dirName)

Error output:

Traceback (most recent call last):
  File "C:\Users\Jennifer\Documents\BatchScripts\unzip.py", line 22, in <module>

    zFile= zipfile.ZipFile(fileName)
  File "C:\Python33\lib\zipfile.py", line 933, in __init__
    self._RealGetContents()
  File "C:\Python33\lib\zipfile.py", line 970, in _RealGetContents
    endrec = _EndRecData(fp)
  File "C:\Python33\lib\zipfile.py", line 237, in _EndRecData
    fpin.seek(0, 2)
AttributeError: 'list' object has no attribute 'seek'

In your file and target directory code blocks, dialog.selectedFiles() returns a list . zipfile.ZipFile can only handle one file at a time, hence your error. To iterate over the list being provided by dialog.selectedFiles() , use the following:

for archive in fileName: # you should probably change it to fileNames to reflect its true nature
    zfile = zipfile.ZipFile(archive)
    print("Extracting " + str(zfile.filename) + "...")
    zfile.extractall(dirName[0]) # also a list, extract to first item and ignore rest

and you should be all set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM