简体   繁体   English

Python-为什么我看到此输出?

[英]Python - Why Am I Seeing This Output?

So I'm getting into Python and I'm writing a script to: 因此,我开始使用Python,并且正在编写脚本以:

  1. Download an RPM using urllib.urlretrieve. 使用urllib.urlretrieve下载RPM。
  2. Extract the files using rpm2cpio and cpio. 使用rpm2cpio和cpio提取文件。
  3. Do some stuff with the files. 对文件进行一些处理。
  4. Clean up using shutil.rmtree. 使用shutil.rmtree进行清理。

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是非阻塞的 ,因此您基本上具有竞争条件-在对Popenrmtree调用之间,无法保证这些进程可以在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.

相关问题 为什么我在python控制台中看到从写入文件的输出? - why am I seeing output in python console from writing to a file? 为什么执行python脚本后在终端上看不到输出? - Why am I not seeing output in the terminal after executing python script? python split,为什么我在输出中看不到所有的连字符 - python split, why am I not seeing all the hyphens in the output 为什么我没有通过Python中的多处理看到加速? - Why am I not seeing speed up via multiprocessing in Python? Python bs4:为什么我只看到 HTML 的一部分? - Python bs4: why am I only seeing part of the HTML? 为什么我没有看到Numpy的DeprecationWarning? - Why am I not seeing Numpy's DeprecationWarning? 为什么我在使用Python 2.7的十进制函数时看不到期望值? - Why am I not seeing what I expect when I use Python 2.7's decimal function? 为什么我在 python 脚本中看到未知数据帧? 以及如何避免让脚本保存它们? - why am I seeing unknown dataframes in my python script? and how to avoid letting the script saving them? Output 在 python 中看不到? - Output not seeing in python? 为什么会出现“对等连接重置”错误? - Why am I seeing 'connection reset by peer' error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM