Read/manipulate file name error: Attributeerror: 'list' Object has no attribute 'Seek'

Asked

Viewed 102 times

0

Hello ! I am trying to create a code in which to read a zipped file and extract all the folders inside this zip. The first point is that this downloaded zip file always comes with a random name of combinations of numbers and letters and did not know how to do it, so I found a tutorial on the internet that shows how to locate some files in some folders by file type and tried to adapt to my problem, and I got to the following code:

removed as a result of seg.

Is there any other way to try to do what I’m trying to do?

1 answer

1

To documentation states that the first parameter of ZipFile must be:

  1. A string with the path to a ZIP archive

     import zipfile
     zf = zipfile.Zipfile("path/para/arquivo.zip")
    
  2. An object file-like

     import zipfile
     with open("path/para/arquivo.zip", rb) as f:
         zf = zipfile.Zipfile(f)
    
  3. An object Path-like

     import zipfile
     from pathlib import Path
    
     p = Path("path/para/arquivo.zip")
     zf = zipfile.Zipfile(p)
    

And in your case, it’s none of them, because you’re passing a list of paths. The module ends up treating its list as a file, and mistakenly tries to invoke the method seek belonging to objects file-like.

For your code to work, just run your code for each element of the list instead of the entire list.

Your modified example would be:

for path in encontraArquivosEmPastaRecursivamente('.zip', 'C:\\Users\\x\\Downloads'):
    zipado = zipfile.ZipFile(path)
    zipado.extractall()

PS: It is worth checking the method security note extractall, follows a free translation:

Warning: Never extract files from unreliable sources without proper inspection. It is possible that files are created outside the folder, for example members that have absolute filenames starting with / or names with ... This module tries to prevent this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.