简体   繁体   English

在子流程中同时使用read()和readlines()

[英]Use both read() and readlines() with subprocess

I currently have the following: 我目前有以下内容:

    filecmd = subprocess.Popen(['git', 'diff', commit, commit2, file],
                                stdout=subprocess.PIPE, stderr=null)
    gitoutraw = filecmd.stdout.read()
    gitoutrawlines = filecmd.stdout.readlines()

This causes gitoutrawlines to be blank. 这导致gitoutrawlines为空白。 How can I set both variables? 如何设置两个变量? I've tried using just gitoutraw = filecmd.stdout.read() and converting it into a list, but I've been unsuccessful. 我尝试仅使用gitoutraw = filecmd.stdout.read()并将其转换为列表,但未成功。

gitoutrawlines = gitoutraw.split("\\n")

if you do : 如果您这样做:

output,error = filecmd.communicate()

communicate give tuple of (stdoutdata,stderror) communicate (stdoutdata,stderror)的元组

output will contain the desired result. 输出将包含所需的结果。 Now you can manipulate it as you want. 现在,您可以根据需要对其进行操作。

you can use the Tony way 你可以用托尼的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM