“静默”提取 7-Zip 文件- 命令行选项
我想在 Python 脚本中提取 7-Zip 存档。它工作得很好,除了它会吐出提取细节(在我的例子中这是巨大的)。
有没有办法在提取时避免这些冗长的信息?我没有找到 7z.exe 的任何“静默”命令行选项。
我的命令是
7z.exe -o some_dir x some_archive.7z
I want to extract a 7-Zip archive in a Python script. It works fine except that it spits out the extraction details (which is huge in my case).
Is there a way to avoid this verbose information while extracting? I did not find any "silent" command line option to 7z.exe
.
My command is
7z.exe -o some_dir x some_archive.7z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我刚刚在搜索相同内容时遇到了这个问题,但我自己解决了!假设使用 Windows/DOS 处理该命令,一个更简单的解决方案是将命令更改为:
即,将输出定向到空文件而不是屏幕。
或者您可以将输出通过管道传输到 DOS“find”命令,以仅输出特定数据,也就是说,
这只会产生以下输出。
或
我的最终解决方案是将命令更改为
“ing”后注意双空格。这导致了以下输出。
这消除了单个文件处理,但生成整体操作的摘要,无论操作类型如何。
I just came across this when searching for the same, but I solved it myself! Assuming the command is processed with Windows / DOS, a simpler solution is to change your command to:
That is, direct the output to a null file rather than the screen.
Or you could pipe the output to the DOS "find" command to only output specific data, that is,
This would just result in the following output.
or
My final solution was to change the command to
Note double space after 'ing'. This resulted in the following output.
This removes the individual file processing, but produces a summary of the overall operation, regardless of the operation type.
一种可能是使用
popen
生成子进程,因此它的输出将返回到父级进行处理/显示(如果需要),否则完全被忽略(使用stdout=PIPE
和stderr 创建您的
以便能够检索子级的输出)。popen
对象=PIPEOne possibility would be to spawn the child process with
popen
, so its output will come back to the parent to be processed/displayed (if desired) or else completely ignored (create yourpopen
object withstdout=PIPE
andstderr=PIPE
to be able to retrieve the output from the child).正如他们所说,要隐藏大部分屏幕填充消息,您可以使用... some_archive.7z | FIND /V“正在压缩”,但“FIND”也会删除包含该单词的错误消息。你不会被警告。由于更新的 7-zip 版本,“FIND”也可能需要更改。
7-zip 具有强制详细输出、无静默模式、混合 stderr 和 stdout(*)、不保存 Unix 权限等。与例如,“tar+bzip2”或“zip”。
(*) “上游(伊戈尔·巴甫洛夫)不想对消息进行不同的输出,尽管他已被多次要求这样做:(” http://us. Generation-nt.com/answer/bug-346463-p7zip-stdout-stderr-help-166693561 .html - “伊戈尔·巴甫洛夫不想改变这种行为”http://sourceforge.net/tracker/?func=detail&aid=1075294&group_id=111810&atid=660493
Like they said, to hide most of the screen-filling messages you could use ... some_archive.7z | FIND /V "Compressing" but that "FIND" would also remove the error messages that had that word. You would not be warned. That "FIND" also may have to be changed because of a newer 7-zip version.
7-zip has a forced verbose output, no silence mode, mixes stderr and stdout(*), doesn't save Unix permissions, etc. Those anti-standards behaviors together put "7-zip" in a bad place when being compared to "tar+bzip2" or "zip", for example.
(*) "Upstream (Igor Pavlov) does not want to make different outputs for messages, even though he's been asked several times to do so :(" http://us.generation-nt.com/answer/bug-346463-p7zip-stdout-stderr-help-166693561.html - "Igor Pavlov does not want to change this behaviour" http://sourceforge.net/tracker/?func=detail&aid=1075294&group_id=111810&atid=660493
如果您从 Powershell 运行 7-zip.exe,并且只想查看错误,那么您可以尝试如下操作:
这只会显示“错误”消息行和周围的 10 行(或任何数字)捕获错误特定的输出。
If you're running 7-zip.exe from Powershell, and you only want to see errors, then you could try something like this:
This will only display the "Error" message line and the surrounding 10 lines (or whatever number) to capture the error specific output.
7zip 没有用于命令行提取的显式“安静”或“静默”模式。
一种可能性是使用 popen 生成子进程,因此其输出将返回到父进程进行处理/显示(如果需要),否则完全被忽略(使用 stdout=PIPE 和 stderr=PIPE 创建 popen 对象,以便能够检索孩子的输出)。
否则尝试这样做:
7zip does not have an explicit "quiet" or "silent" mode for command line extraction.
One possibility would be to spawn the child process with popen, so its output will come back to the parent to be processed/displayed (if desired) or else completely ignored (create your popen object with stdout=PIPE and stderr=PIPE to be able to retrieve the output from the child).
Otherwise Try doing this:
扩展@Matthew的答案和这个答案https://superuser。 com/questions/194659/how-to-disable-the-output-of-7-zip
我使用 FINDSTR 而不是 find,因此我可以链接多行来排除和空行:
我使用 /C 以便space 是一个空格,否则它是要排除的多个单词之间的分隔符,就像在这个更简单的版本中一样:(
存在相同的警告,如果新版本中的措辞发生变化,或者如果有用的行以单词“压缩”开头,它将没有按预期工作)。
Expanding on @Matthew 's answer and this answer https://superuser.com/questions/194659/how-to-disable-the-output-of-7-zip
I'm using FINDSTR instead of find so I can chain multiple lines to exclude and blank lines as well:
I'm using /C so that a space is a space, otherwise it's a separator between multiple words to exlude as in this simpler version:
(the same caveats exist, if the wording changes in a new version, or if a useful line starts with the word "Compressing ", it will not work as expected).
<代码>| FIND 是一个很好的替代方案,可以在不显示无关紧要的文本的情况下显示发生的情况。
The
| FIND
is a good alternative to show what happened without displaying insignificant text.检查 7zip 源代码我发现隐藏的
-ba
开关似乎可以解决问题。不幸的是它还没有完成。我设法通过对源代码的多次修改使其工作,但这只是一个 hack。如果有人感兴趣,选项变量称为options.EnableHeaders
,并且需要在CPP/7zip/UI/Console/Main.cpp
文件中进行更改。或者,您也可以在 tracker 中联系 7Zip 的作者来完成该功能。对此有多个请求,其中之一位于此处。
Examining 7zip source I found hidden
-ba
switch that seems to do the trick. Unfortunately it is not finished. I managed to make it work with several modifications of sources but it's just a hack. If someone's interested, the option variable is calledoptions.EnableHeaders
and changes are required inCPP/7zip/UI/Console/Main.cpp
file.Alternatively you can poke 7Zip's author to finish the feature in tracker. There are several requests on this and one of them is here.
仅显示最后 4 行...
给我:
开关 -y 是对所有内容回答“是”(在我的情况下覆盖现有文件)。
To show just the last 4 lines...
gives me:
The switch -y is to answer yes to everything (in my case to override existing files).
7-zip没有这样的选项。另外,在每个压缩文件中打印的行应该显示在同一位置,而无需换行,从而擦除前一个,这具有很酷的效果。不幸的是,在某些情况下(詹金斯......)它产生了几行☹️淹没控制台。
NUL
(Windows)可能是一种解决方案。7-zip has not such an option. Plus the lines printed at each file compressed are supposed to display at the same spot without newline, erasing the previous one, which has a cool effect. Unfortunatly, in some contexts (Jenkins...) it produced several lines ☹️ flooding the console.
NUL
(windows) is maybe one solution.在类 Unix 操作系统(Linux、BSD 等)上,shell 命令
7z ... >/dev/null
将丢弃由7z
写入到 <强>标准输出。这应该涵盖7z
编写的所有状态/信息消息。似乎
7z
将错误消息写入标准错误,因此如果您执行>/dev/null
,错误消息仍然会显示。On Unix-like operating systems (Linux, BSD, etc.) the shell command
7z ... >/dev/null
will discard all text written by7z
to standard output. That should cover all the status/informational messages written by7z
.It seems that
7z
writes error messages to standard error so if you do>/dev/null
, error messages will still be shown.正如上面 Fr0sT 所说, -ba 开关仅输出有效的内容(至少在我尝试的列表选项中)。
7z.exe l archive_name.zip
7z.exe l -ba archive_name.zip
有很大的不同,特别是用于解析脚本中的输出。
无需修改任何内容,只需使用 version19 中的 -ba 开关即可。这一点上面也有人说过了。我将作为答案,因为我无法发表评论。
As told by Fr0sT above, -ba switch outputs only valid things (at least in list option on which I was trying).
7z.exe l archive_name.zip
7z.exe l -ba archive_name.zip
made great difference, esp for parsing the output in scripts.
There is no need to modify anything, just use -ba switch in version19. This was also told bysomeone above. I'm putting as answer as I can't comment.
您可以使用 -y 开关来阻止 7-Zip 显示提示。这将对所有提示回答“是”。仅当您有信心时才使用此功能。
You can stop 7-Zip from displaying prompts by using the -y switch. This will answer yes to all prompts. Use this only when you are confident.