这是经过一些尝试后的最终版本。以前没有用,所以我将其删除,而不是附加。读到最后,因为最终解决方案可能不需要所有内容。
进入主题。我会使用 Python。如果这是一次任务,那么它可能是矫枉过正,但在任何其他情况下 - 您可以记录所有步骤以供将来调查,正则表达式,编排一些命令以提供输入,以及获取和处理输出 - 每次。所有这些情况在 Python 中都很容易。如果你有的话。
现在,我将写下如何拥有 env。配置。并非所有都是强制性的,但尝试安装会执行一些步骤,也许对过程的描述本身可能会有所帮助。
我有MinGW - 32 位版本。然而,提取 7zip 并不是强制性的。安装后转到C:\MinGW\bin 并运行mingw-get.exe:
-
Basic Setup 我已经安装了msys-base(右键单击,标记安装,从安装菜单 - 应用更改)。这样我就有了 bash、sed、grep 等等。
- 在
All Packages 中有mingw32-libarchive with dll as class. Since pythonlibarchive` 包只是一个包装器,您需要这个dll 来实际包装二进制文件。
示例适用于 Python 3。我使用的是 32 位版本。你可以在他们的主页上fetch它。我已经安装在默认目录中,这很奇怪。所以建议安装在磁盘的根目录中——比如 mingw。
其他 - conemu 比默认控制台好得多。
在 Python 中安装包。 pip 用于此目的。从您的控制台转到 Python 主页,那里有 Scripts 子目录。对我来说是:c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\Scripts。您可以使用例如pip search archive 进行搜索,并使用pip install libarchive-c 进行安装:
> pip.exe install libarchive-c
Collecting libarchive-c
Downloading libarchive_c-2.7-py2.py3-none-any.whl
Installing collected packages: libarchive-c
Successfully installed libarchive-c-2.7
在cd ..调用python之后,就可以使用/导入新库了:
>>> import libarchive
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\__init__.py", line 1, in <module>
from .entry import ArchiveEntry
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\entry.py", line 6, in <module>
from . import ffi
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\ffi.py", line 27, in <module>
libarchive = ctypes.cdll.LoadLibrary(libarchive_path)
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 426, in LoadLibrary
return self._dlltype(name)
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
TypeError: LoadLibrary() argument 1 must be str, not None
所以它失败了。我试图解决这个问题,但失败了:
>>> import libarchive
read format "cab" is not supported
read format "7zip" is not supported
read format "rar" is not supported
read format "lha" is not supported
read filter "uu" is not supported
read filter "lzop" is not supported
read filter "grzip" is not supported
read filter "bzip2" is not supported
read filter "rpm" is not supported
read filter "xz" is not supported
read filter "none" is not supported
read filter "compress" is not supported
read filter "all" is not supported
read filter "lzma" is not supported
read filter "lzip" is not supported
read filter "lrzip" is not supported
read filter "gzip" is not supported
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\__init__.py", line 1, in <module>
from .entry import ArchiveEntry
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\entry.py", line 6, in <module>
from . import ffi
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\ffi.py", line 167, in <module>
c_int, check_int)
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\libarchive\ffi.py", line 92, in ffi
f = getattr(libarchive, 'archive_'+name)
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 361, in __getattr__
func = self.__getitem__(name)
File "c:\Users\<<username>>\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 366, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'archive_read_open_filename_w' not found
尝试使用set 命令直接提供信息,但失败了......所以我搬到了pylzma - 因为不需要mingw。 pip 安装失败:
> pip.exe install pylzma
Collecting pylzma
Downloading pylzma-0.4.9.tar.gz (115kB)
100% |--------------------------------| 122kB 1.3MB/s
Installing collected packages: pylzma
Running setup.py install for pylzma ... error
Complete output from command c:\users\texxas\appdata\local\programs\python\python36-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\texxas\\AppData\\Local\\Temp\\pip-build-99t_zgmz\\pylzma\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\texxas\AppData\Local\Temp\pip-ffe3nbwk-record\install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build\lib.win32-3.6
copying py7zlib.py -> build\lib.win32-3.6
running build_ext
adding support for multithreaded compression
building 'pylzma' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
再次失败。但这很简单——我已经安装了 2015 年的 Visual Studio 构建工具,并且工作正常。我安装了sevenzip,所以我创建了示例存档。所以最后我可以启动 python 并做:
from py7zlib import Archive7z
f = open(r"C:\Users\texxas\Desktop\try.7z", 'rb')
a = Archive7z(f)
a.filenames
得到了空列表。仔细观察......可以更好地理解 - pylzma 不考虑空文件 - 只是为了让您意识到这一点。因此,将一个字符放入我的示例文件中,最后一行给出:
>>> a.filenames
['try/a/test.txt', 'try/a/test1.txt', 'try/a/test2.txt', 'try/a/test3.txt', 'try/a/test4.txt', 'try/a/test5.txt', 'try/a/test6.txt', 'try/a/test7.txt', 'try/b/test.txt', 'try/b/test1.txt', 'try/b/test2.txt', 'try/b/test3.txt', 'try/b/test4.txt', 'try/b/test5.txt', 'try/b/test6.txt', 'try/b/test7.txt', 'try/c/test.txt', 'try/c/test1.txt', 'try/c/test11.txt', 'try/c/test2.txt', 'try/c/test3.txt', 'try/c/test4.txt', 'try/c/test5.txt', 'try/c/test6.txt', 'try/c/test7.txt']
所以...休息是小菜一碟。实际上这是原始帖子的一部分:
import os
import py7zlib
for folder, subfolders, files in os.walk('.'):
for file in files:
if file.endswith('.7z'):
# sooo 7z archive - extract needed.
try:
with open(file, 'rb') as f:
z = py7zlib.Archive7z(f)
for file in z.list():
if arch.getinfo(file).filename.endswith('*.py'):
arch.extract(file, './dest')
except py7zlib.FormatError as e:
print ('file ' + file)
print (str(e))
附带说明 - Anaconda 是很棒的工具,但完全安装需要 500+MB,所以这太多了。
也让我分享wmctrl.py工具,来自我的github:
cmd = 'wmctrl -ir ' + str(active.window) + \
' -e 0,' + str(stored.left) + ',' + str(stored.top) + ',' + str(stored.width) + ',' + str(stored.height)
print cmd
res = getoutput(cmd)
这样你就可以编排不同的命令——这里是wmctrl。可以以允许数据处理的方式处理结果。