【问题标题】:Os walk directory return string literal for unzippingos walk 目录返回用于解压缩的字符串文字
【发布时间】:2018-03-24 01:57:24
【问题描述】:

问题出现在字符串变量试图被 zipfile.Zipfile(variable) 读取的地方。回调是文件不存在。这是因为它为每个文件夹添加了一个额外的 \。我尝试了几件事来尝试将字符串变成文字,但是当我这样做时,它会打印一个额外的 \\\.我什至尝试将其设为文字,但正如我所期望的那样,它会重复返回 \\\\\。任何见解都将不胜感激!!! 代码结构如下,这里是回调函数

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\\\\\\\

代码:

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()

【问题讨论】:

  • 这甚至不是合法代码(原始字符串不能以反斜杠结尾,因此r'C:\Users\Yume\AppData\' 在运行之前将是SyntaxError;StackOverflow 自己的语法突出显示清楚地说明了这一点)。在字符串化的list 上运行正则表达式匹配函数是荒谬的。需要发minimal reproducible example,这里强调可验证;您发布的代码显然以与您的回溯无关的方式被破坏。
  • 您显然也误解了os.walk 产生的第二个和第三个值;它们都是lists,因此尝试对它们执行字符串操作并期望它生成有用的路径(尤其是尝试连接子文件夹和文件名,它们首先位于目录树的同一级别)疯了。
  • 你说得对,我不明白我是新来的……这就是我问的原因……我现在为寻求帮助而感到难过:(我仍然没有看到除了我认为自己的其他任何策略,您可能有什么建议吗?此外,我尝试过的 100 个回溯都是相同的,如果它们不符合语法,我深表歉意。或者更多地侮辱我?@ShadowRanger
  • 我们的目标不是侮辱,而是指出你咬的比你能嚼的多。你要么没有足够的 Python 经验来做你想做的事情(在这种情况下,你需要通过课程、教程或其他东西来获得基本的掌握,比如你如何使用 list s) 或者你有能力,但你没有阅读os.walk 文档(它给出了简单的例子,并解释了它是如何工作的)。您发布的内容充满了问题,并且与回溯无关,因此无法提供有用的答案。
  • @ShadowRanger 感谢您的严厉指导。我的建议很简单,例如...遍历列表...。或者可能是 os.path.join 而不是字符串连接...会很棒。我不认为我咬得比我能咀嚼的多,因为我看不到任何其他真正接近这一点的方法。也许您的无限智慧可以为我指出执行相同任务的更精确的方法。我是 python 新手,并不愚蠢。我不知道最佳实践,文档可能非常模糊......我很少喜欢寻求帮助

标签: string python-3.x data-structures zipfile os.walk


【解决方案1】:

通过在 os walk 创建的文件列表中运行 for 循环,我的问题得到了纠正。我清理了我的代码,这是一个工作程序,它遍历目录找到压缩的 .zip 或 .rar 并将其解压缩到指定的目的地。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多