0

I am trying to use the following code to unzip all the zip folders in my root folder; this code was found on this thread:

Unzip zip files in folders and subfolders with python

rootPath = u"//rootdir/myfolder" # CHOOSE ROOT FOLDER HERE
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        print(os.path.join(root, filename))
        zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))

but I keep getting this error that says FileNotFoundError saying the xlsx file does not exist:

Traceback (most recent call last):
  File "//rootdir/myfolder/Python code/unzip_helper.py", line 29, in <module>
    zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
  File "//rootdir/myfolder/Python\Python36-32\lib\zipfile.py", line 1491, in extractall
    self.extract(zipinfo, path, pwd)
  File "//myaccount/Local\Programs\Python\Python36-32\lib\zipfile.py", line 1479, in extract
    return self._extract_member(member, path, pwd)
  File "//myaccount/Local\Programs\Python\Python36-32\lib\zipfile.py", line 1542, in _extract_member
    open(targetpath, "wb") as target:

FileNotFoundError: [Errno 2] No such file or directory: '\\rootdir\myfolder\._SGS Naked 3 01 WS Kappa Coated and a very long very long file name could this be a problem i dont think so.xlsx'

My question is, why would it want to unzip this excel file anyways?!

And how can I get rid of the error?

I've also tried using r instead of u for rootPath:

rootPath = r"//rootdir/myfolder"

and I get the same error.

Any help is truly appreciated!

alwaysaskingquestions
  • 1,595
  • 5
  • 22
  • 49
  • I believe `os.path.join(root, os.path.splitext(filename)[0])` is the reason. – GIZ Jul 27 '17 at 20:21
  • what do you mean by that? but it works on other zip folders. it unzipped about 20 zip folders successfully – alwaysaskingquestions Jul 27 '17 at 20:22
  • The error explicitly states that there's no such file or directory. In other words, it's not trying to unzip `.xlsx` file but rather complaining that there's no such directory to unzip to. Even if you sucessfully unzipped some archives this will not guarantee the `os.path.join(root, os.path.splitext(filename)[0])` will give a valid path to unzip the archive all the time. – GIZ Jul 27 '17 at 20:25
  • Have you tried this: [python win32 filename length workaround](https://stackoverflow.com/a/3557977/1202745). I have no windows now and cannot check is the MAX_PATH issue still present in py3.6 – robyschek Jul 27 '17 at 20:34
  • @direprobs but sa far as i have checked, that file IS there! (without the "._" beginning", or if it is referring to the file starting w "._", then the file IS NOT there!) – alwaysaskingquestions Jul 27 '17 at 21:06
  • @robyschek you may be right, because i cannot even open the file; excel would complain that the file name is too long. – alwaysaskingquestions Jul 27 '17 at 21:06
  • It's easy to check: `if len(path_to_xlsx > 260): print("path too long:", path_to_xlsx)`. MAX_PATH is 260 chars since beginning of windows . – robyschek Jul 27 '17 at 21:12
  • @alwaysaskingquestions again with or without extra dots you'll have some failures and some successes in your code that's should be intuitive; you're relying on the dynamic behavior of your code. Change the last line to a hard-coded path and see what happens. – GIZ Jul 28 '17 at 08:03

1 Answers1

1

Some filenames and directory names may have extra dots in their names, as a consequence the last line, unlike Windows filenames can have dots on Unix:

zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))

this line fails. To see how that happens:

>>> filename = "my.arch.zip"
>>> root = "/my/path/to/mydir/"
>>> os.path.join(root, os.path.splitext(filename)[0])
'/my/path/to/mydir/my.arch'

With or without extra dots, problems will still take place in your code:

>>> os.path.join(root, os.path.splitext(filename)[0])
'/my/path.to/mydir/arch'

If no '/my/path.to/mydir/arch' can be found, FileNotFoundError will be raised. I suggest that you be explicit in you path, otherwise you have to ensure the existence of those directories.

ZipFile.extractall(path=None, members=None, pwd=None)

Extract all members from the archive to the current working directory. path specifies a different directory to extract to...

Unless path is an existent directory, FileNotFoundError will be raised.

GIZ
  • 4,409
  • 1
  • 24
  • 43