-1

The problem occurs where the string variable tries to get read by zipfile.Zipfile(variable). The callback is file does not exist. This is because it is adding an extra \ to each folder. I tried several things to try and make the string into a literal but when I do this it prints an extra \\\. I tried even making that a literal but as I would expect it repeated the same thing returning \\\\\.Any insight would be greatly appreciated!!! The code structure is below and here are the callback functions

Traceback (most recent call last):
  File "C:\Users\Yume\Desktop\Walk a Dir Unzip and copy to.py", line 17, in <module>
    z = zipfile.ZipFile(zf, 'r')
  File "C:\Users\Yume\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: "C:\\Users\\Yume\\AppData\\Roaming\\

Traceback (most recent call last):
  File "C:\Users\Yume\Desktop\Walk a Dir Unzip and copy to.py", line 17, in <module> z = zipfile.ZipFile(rzf, 'r')
  File "C:\Users\Yume\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
    self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: '"C:\\\\Users\\\\Yume\\\\AppData\\\\Roaming\\\\


   z = zipfile.ZipFile(rrzf, 'r')
  File "C:\Users\Yume\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
    self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: '\'"C:\\\\\\\\Users\\\\\\\\Yume\\\\\\\\AppData\\\\\\\\Roaming\\\\\\\

CODE:

import os
import re
from shutil import copyfile
import zipfile
import rarfile

isRar = re.compile('.+\\.rar')
isZip = re.compile('.+\\.zip')
for folderName, subfolders, filenames in os.walk(r'C:\Users\Yume\AppData'):

    if isZip.match(str(filenames)):
    zf =  folderName+ str(subfolders)+str(filenames)
    rzf = '%r'%zf
    z = zipfile.ZipFile(zf)
    z.extractall(r'C:\Users\Yume')
    z.close()                   
    if isRar.match(str(filenames)):
    rf = folderName+ str(subfolders)+str(filenames)
    rrf = "%r"%rf
    r = rarfile.RarFile(rf)
    r.extractall(r'C:\Users\Yume')  
    r.close()
  • This isn't even legal code (raw strings can't end with a backslash, so `r'C:\Users\Yume\AppData\'` would be a `SyntaxError` before it even ran; StackOverflow's own syntax highlighting makes this clear). And running a regex matching function on a stringified `list` is nonsensical. You need to post a [MCVE], emphasis on verifiable here; the code you've posted is clearly broken in ways that have nothing to do with your traceback. – ShadowRanger Mar 24 '18 at 02:01
  • You also clearly seem to misunderstand the second and third values yielded by `os.walk`; they're both `list`s, so trying to perform string operations with them and expecting it to make useful paths (especially trying to concatenate the subfolders and the filenames, which are at the same level of the directory tree in the first place) is insane. – ShadowRanger Mar 24 '18 at 02:04
  • You're right I don't understand I'm new at this... that's why I asked... I feel bad for asking for help now :( and I still don't see a strategy anywhere besides this which I thought of myself do you have a suggestion possibly? Also the tracebacks were all the same out of the 100's I tried I apologize if they dont match syntax. Or insult me more? @ShadowRanger – Brandoncbaldwin Mar 24 '18 at 02:12
  • The goal is not to be insulting, but to point out that you're biting off more than you can chew. You're either not sufficiently experienced with Python to do what you want to do at all (in which case you need to run through a class, tutorial, or something else, to get a basic grasp of stuff like how you use `list`s) or you're competent, but you are not reading the `os.walk` documentation (which gives simple examples, and explains how it actually works). What you posted is so riddled with problems, and unrelated to the traceback, that it's impossible to provide useful answers. – ShadowRanger Mar 24 '18 at 02:58
  • @ShadowRanger thanks for your harsh guidance. I simple suggestion like...loop through the list.... or maybe os.path.join instead of string concatenation... would've been great. I don't think I'm biting off more than I can chew as I don't see any other way of really approaching this. Perhaps your infinite wisdom can point me to a more much precise way of performing this same task. I'm new to python not stupid. I don't know best practices and the documentation can be very vague... I rarely like asking for help – Brandoncbaldwin Mar 27 '18 at 14:05

1 Answers1

0

My problem was corrected by running a for loop through the file list created by os walk. I cleaned up my code and here is a working program that walks a directory find a compressed .zip or .rar and extracts it to the specified destination.

import os
import zipfile
import rarfile
import unrar

isRar = re.compile('.*\\.rar')
isZip = re.compile('.*\\.zip')
for dirpath, dirnames, filenames in os.walk(r'C:\Users\'):
  for file in filenames:
    if isZip.match(file):
        zf = os.path.join(dirpath, file)
        z= zipfile.ZipFile(zf)
        z.extractall(r'C:\Users\Unzipped')
        z.close()
    if isRar.match(file):
        rf = os.path.join(dirpath, file)
        r = rarfile.RarFile(rf)
        r.extractall(r'C:\Users\Unzipped')
        r.close()