1

I want to extract a file within a .zip archive to another directory. First I create a ZipFile object

  zfile = '/home/.../filename.zip'
  archive = zipfile.ZipFile(zfile, 'r')

The triple dot ... is me just hiding the full path, not there in the real path.

Then I extract a particular member from the archive to another directory

  print(archive.namelist()[0])    
  # returns sub\\xxx.data where the two back slashes is not a typo!
  path = '/home/.../datadir'
  archive.extract(member='sub\\xxx.data', path=path)

Then I get a system error

  OSError: [Errno 22] Invalid argument: '/home/.../datadir/sub\\xxx.data'

If I manually change the two back slashes \\ to one forward slash / then I get a different error

  archive.extract(member='sub/xxx.data', path=path)

  KeyError: "There is no item named 'sub/xxx.data' in the archive"

So the Linux system is not recognizing the path with two back slashes as a valid Linux path, and the path cannot be changed manually because then the file within the .zip archive is not recognized at all.

I get the same issue when using 7-Zip

Unfortunately I do not have any information or control regarding the method used to create the .zip file in the first place.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Russell Burdt
  • 2,391
  • 2
  • 19
  • 30
  • 1
    It looks like the path is using both forward and back slashes. Since you're on linux I would try the path with all forward slashes. Also, the triple dot ... in your path might not be supported by the shell you're using unless you know how to activate it. – mindless-overflow Jan 21 '20 at 00:45
  • @mindless-overflow The triple dot ... is just me hiding the full path. In reality there is no triple dot ... in any paths. I will edit the main post to say that. – Russell Burdt Jan 21 '20 at 00:48
  • 1
    I would say that the issue probably lies within your path having different slashes then! Try it out manually with all forward slashes. – mindless-overflow Jan 21 '20 at 00:57
  • @mindless-overflow thanks for the comment and I've updated the post to address that adding a forward slash leads to a different problem. – Russell Burdt Jan 21 '20 at 01:28
  • 1
    You didn't just change double backslashes to a forward slash, you also changed `path=path` to `path=rbin`. I don't know if this is relevant. – mkrieger1 Jan 21 '20 at 01:39
  • What if you use `archive.extract(member=archive.namelist()[0], path=path)`? And what if ``archive.extract(member='sub\\\\xxx.data', path=path)``? – mkrieger1 Jan 21 '20 at 01:44
  • @mkrieger1 The ```path=rbin``` was a typo and it should have been ```path=path```. Now edited and thank you for catching that. – Russell Burdt Jan 21 '20 at 05:18
  • @mkrieger1 When I try ```archive.extract(member=archive.namelist()[0], path=path)``` I get ```OSError: [Errno 22] Invalid argument: '/home/.../datadir/sub\\xxx.data'``` – Russell Burdt Jan 21 '20 at 18:04
  • @mkrieger1 And when I try ```archive.extract(member='sub\\\\xxx.data', path=path)``` I get ```KeyError: "There is no item named 'sub\\\\\\\\xxx.data' in the archive"``` I have also tried to add `r` in front of paths and other combinations all without success. – Russell Burdt Jan 21 '20 at 18:06

3 Answers3

4

Linux recognises only '/' as a path separator, but you can set os.altsep = '\\' which should work.

Russell Burdt
  • 2,391
  • 2
  • 19
  • 30
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • ```with zipfile.ZipFile(zfile,'r') as z:``` no problem. ```with open(os.path.join(path,z.namelist()[0]),'wb') as fou:``` creates same error as described above: ```OSError: [Errno 22] Invalid argument: '/home/.../datadir/sub\\xxx.data'``` – Russell Burdt Jan 21 '20 at 18:16
  • 1
    @RussellBurdt: ah, thanks. Linux recognises only '/' as a path separator, but you can set `os.altsep = '\\'` which should work. Let me know. – mechanical_meat Jan 21 '20 at 18:44
  • 1
    That was it. Thank you! Please peer review my edits to your original answer so I can upvote. – Russell Burdt Jan 21 '20 at 19:40
0

Error 22 may also apply for a corrupt original zip file. Assure that your "filename.zip" file is a valid zip file.

0

The answer by mechanical_meat and Russel Burdt is the correct approach, but in my case os.path.altsep= '\\' was the solution.

Agi
  • 11
  • 1
  • 4