lgjmac
lgjmac

Reputation: 133

Using Tee-Object to output the result of a python script to both terminal and file

The problem is that I cannot record the output of my Python script using Tee-Object to both terminal and a file.

I have multiple files containing lines of data. I need to verify this data via an HTTP request to a server. There are 8 large files and given I expect this will take a day to run since I don't want to flood the server.

  1. Importing file strings and running Python script is producing output to terminal. Completed as follows:

    $db = Import-Csv C:\Users\xxxx\documents\bunnies\foo.txt
    foreach ($i in $db.StringName) {
        & py -2.7 myscript.py -option $i
    }
    

    $db is the file. $i is the string (line) in the file. script prints to terminal.

    Since the output is going to be over several days, I need to know that it will be recorded. Tee-Object has not created a file after an hour of output.

    foreach ($i in $db.StringName) {
        & py -2.7 myscript.py -option $i
    } > Tee-Object -FilePath .../bunnyrabbit.txt
    
  2. I assume that > Tee-Object -FilePath .../bunnyrabbit.txt appended should create the file immediately and write in an ongoing manner?

    I need to be able to check the output is okay as the program runs.

Additional: filtering output

The output per line of the script is simply "x is correct" or "x is incorrect". If I want to filter all the corrects into one file and the incorrects into another how to go about this?

My original plan was simply to re-read the output file in python and do it in a language I know.

Upvotes: 2

Views: 1412

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

You'll need the | pipeline operator, not the > redirection operator.

Additionally, either move Tee-Object inside the foreach body and use the -Append switch, or change the script to use the ForEach-Object cmdlet instead of a loop statement as suggested by Ansgar Wiechers:

$db = Import-Csv C:\Users\xxxx\documents\bunnies\foo.txt
foreach ($i in $db.StringName) {
    & py -2.7 myscript.py -option $i |Tee-Object -FilePath ..\bunnyrabbit.txt -Append
}

or

$db = Import-Csv C:\Users\xxxx\documents\bunnies\foo.txt
$db.StringName |ForEach-Object {
    & py -2.7 myscript.py -option $_ 
} |Tee-Object -FilePath ..\bunnyrabbit.txt

Upvotes: 2

Related Questions