【发布时间】:2021-07-06 15:09:42
【问题描述】:
我尝试在 Python 中存储命令的输出:gunzip -t / tar -t,但我不知道如何。确实,在 shell termianl 中,我可以使用 echo $?但在 Python 中,使用 os.popen() 或 os.system() 是不可能的。
我当前的脚本如下:
os.system("gunzip -t Path_to_tar.gz")
gzip_corrupt = os.popen("echo $?").read().replace('\n','')
os.system("gunzip -c Path_to_tar.gz | tar -t > /dev/null")
tar_corrup = os.popen("echo $?").read().replace('\n','')
print(tar_corrup)
print(gzip_corrupt)
您知道如何在 python 中存储 gunzip -t 的输出吗?
【问题讨论】:
-
不,因为我知道这个解决方案并且不适用于没有真正输出的 tar -t 或 gunzip -t,因为 shell 中的 $(cmd) 无法捕获结果。
-
Python 有支持 tar 和 gzip 格式的库:docs.python.org/3/library/tarfile.htmldocs.python.org/3/library/gzip.html
-
你为什么使用 Python 只是为了运行 shell 命令?这不仅没有必要,而且有潜在的危险。
-
谢谢,但我没有看到 tar 命令的 -t 选项的等效项,它显示存档是否已损坏:)