[Tutor] need help writing subprocess output to file(s)

Cameron Simpson cs at cskk.id.au
Sun Apr 26 22:20:56 EDT 2020


On 26Apr2020 21:46, Matthew Herzog <matthew.herzog at gmail.com> wrote:
>I have to run a utility (sdptool) that checks the firmware versions on 
>>500
>servers.
>What I pasted below works but the stdout needs to be written to a file, one
>per IP address.
>How do I capture the stdout to files? Even a single logfile would work.
>Thanks.
>
>#!/usr/bin/python3
>import subprocess
>
>with open("ips.txt") as ip_list:
>    ips = ip_list.readlines()
>
>for ip in ips:
>    output = subprocess.Popen(["/opt/bin/sdptool", ip, "check"])

Something like this:

    for ip in ips:
        with open(op + '.out', 'w') as output:
            output = subprocess.Popen(["/opt/bin/sdptool", ip, "check"], stdout=output)

which will open an output file and pass it (still open, important) to 
Popen for use as stdout.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list