[英]Python - Why Am I Seeing This Output?
So I'm getting into Python and I'm writing a script to: 因此,我开始使用Python,并且正在编写脚本以:
Functionally it's all good it does do all of that but since I put in the clean up code I'm getting the following output: 从功能上讲,它可以完成所有这些工作,但是由于我输入了清理代码,因此得到以下输出:
rpm2cpio: MyRPM.rpm: No such file or directory
cpio: premature end of archive
Here is the code: 这是代码:
#!/usr/bin/python
from contextlib import contextmanager
import os, subprocess, shutil
@contextmanager
def cd(directory):
startingDirectory = os.getcwd()
os.chdir(os.path.expanduser(directory))
try:
yield
finally:
os.chdir(startingDirectory)
# Extract the files from the RPM to the temp directory
with cd("/tempdir"):
rpm2cpio = subprocess.Popen(["rpm2cpio", "MyRPM.rpm"], stdout=subprocess.PIPE)
cpio = subprocess.Popen(["cpio", "-idm", "--quiet"], stdin=rpm2cpio.stdout, stdout=None)
# Do
# Some
# Things
# Involving
# Shenanigans
# Remove the temp directory and all it's contents
shutil.rmtree("/tempdir")
If you see some syntax issues with the code here (or missing imports or something) please ignore unless it actually pertains to the reason I'm getting the two messages. 如果您在此处看到代码的某些语法问题(或缺少导入或其他内容),请忽略它,除非它实际上与我收到两条消息的原因有关。 I tried to strip down the script to the releveant bits.
我试图将脚本简化为相关的部分。 All I'm looking for is an explanation as to why the above two messages are being printed.
我要寻找的只是关于为什么打印以上两条消息的解释。 I'd have assumed that the script is executed from the top down but now I'm thinking I may be wrong in this case?
我以为脚本是从上到下执行的,但是现在我想我在这种情况下可能错了?
EDIT: It feels like the 'rpm2cpio' and 'cpio' commands are leaving something open as long as the script is running like something I need to explicitly close...? 编辑:感觉就像rpm2cpio和cpio命令正在打开某些东西,只要脚本正在运行,就像我需要显式关闭...一样? Does that make any sense?
这有任何意义吗? :)
:)
Thanks! 谢谢! J
Ĵ
subprocess.Popen
is non-blocking , so you basically have a race condition - between your calls to Popen
and rmtree
there is no guarantee that those processes can finish (or even start!) before rmtree
runs. subprocess.Popen
是非阻塞的 ,因此您基本上具有竞争条件-在对Popen
和rmtree
调用之间,无法保证这些进程可以在rmtree
运行之前完成(甚至启动!)。
I suggest you wait for the Popen objects to return with 我建议您等待Popen对象返回
cpio.wait()
rpm2cpio.wait()
# Remove the temp directory and all it's contents
shutil.rmtree("/tempdir")
Using the blocking subprocess.call
doesn't look like an option for you with how you're piping the commands. 使用阻塞
subprocess.call
对于管道命令的方式看起来并不理想。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.