简体   繁体   中英

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. 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.

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

if you do :

output,error = filecmd.communicate()

communicate give tuple of (stdoutdata,stderror)

output will contain the desired result. Now you can manipulate it as you want.

you can use the Tony way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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