1

So I know how to execute a python script and have it output in command window using os.command or even subprocess. Also i know that capturing output into a text file is done via os.command('my command > me.txt".

My question here is: IS there a way to do both of them with one command? ie execute a python script,capture the output to a text file AND have it show on the command window?

If this is not possible here is another question: the command I want to execute takes up to 8 min to finish and writes up to 300 lines in a text file. Can I access the text file while the command is still executing in order to get some data without waiting for the command to finish executing? like access the text file every 1 minute for example?

If neither is possible then this would also do the job: when my command executes succesfully it prints out a Done statement on the cmd as well as many other lines. Can i check in the cmd if that "Done" string was printed or it needs to be captured in a text file for that dearch to happen?

JJ Adams
  • 481
  • 4
  • 17
  • Are you talking about printing the result of subprocess to both a file and stdout or are you referring to all of the output from the python file itself? Those are two completely different issues. For redirecting all python execution output check out this post: http://stackoverflow.com/questions/14906764/how-to-redirect-stdout-to-file-and-console-with-scripting – Bob Jun 06 '14 at 20:00
  • have you tried this `./script.py > output.txt | type output.txt`? – Pavel Jun 06 '14 at 20:10
  • Bob, I was talking about printing the result of subprocess to both a file and stdout NOT the printing output of python file – JJ Adams Jun 06 '14 at 20:13
  • Pvel, your command crashes everytime I run it. it prints the 1st 2 lines in cmd and in text file and then crashes – JJ Adams Jun 06 '14 at 20:24
  • Perhaps you could replace `stdout` with an instance of the class described in [this](http://stackoverflow.com/a/616686/355230) answer. – martineau Jun 06 '14 at 20:25
  • martineau, i'm having the same issue I had with Pvel. It runs good for the first 30 sec on cmd but then crashes giving an error and in txt file it prints nothing but a number 8 which im guessing is the error number. Guys keep in mind that the command I'm running is a long command to execute and complete so it makes sense why it keeps crashing when it is supposed to execute it on both cmd and txt file (it is like it is calling the command twice in a row).. – JJ Adams Jun 06 '14 at 21:19
  • OK guys this approach might help solve the problem: When my command executes successfully it prints out a Done statement on the cmd as well as many other lines of execution before it. Can i check in the cmd if that "Done" string was printed or it needs to be captured in a text file for that search to happen? – JJ Adams Jun 06 '14 at 21:21
  • you may also check this http://stackoverflow.com/questions/2996887/how-to-replicate-tee-behavior-in-python-when-using-subprocess – wmz Jun 07 '14 at 07:50
  • wmz, your solution was very helpful. It didn't work solving the initial issue, but I was able to use the script to achieve my aim using a different approach – JJ Adams Jun 09 '14 at 17:21

3 Answers3

1

The easiest way to save the output of a command while echoing it to stdout is to use tee:

$ python script.py | tee logfile.log

if you want to follow the output of a file while it is being written use tail:

$ tail -f logfile

you might want to unbuffer or flush the output immediately to be able to read the output before a full line or a buffer is filled up:

$ unbuffer python script.py > logfile
$ tail -f logfile

or

print ("to file..", flush = True)
gauteh
  • 16,435
  • 4
  • 30
  • 34
0

If you can do this from within your script rather than from the command line it would be quite easy.

with open("output.txt", "w") as output:
    print>>output, "what you want as output" #prints to output file
    print "what you want as output" #prints to screen 
ded
  • 420
  • 2
  • 13
  • buddy your scripts kicks in in the cmd but crashes after couple of seconds + it doesn't print anything in txt file. So it works perfectly when im prinitng out a line or two like your example, but when im executing a command with os.system in both ines it crashes. – JJ Adams Jun 06 '14 at 20:33
0

The easier way I devised is to create a function that prints to both screen and to file. The example below works when you input the output file name as an argument:

OutputFile= args.Output_File
if args.Output_File:
    OF = open(OutputFile, 'w')

def printing(text):
    print text
    if args.Output_File:
        OF.write(text + "\n")

#To print a line_of_text both to screen and file all you need to do is:
printing(line_of_text)
hector garcia
  • 109
  • 1
  • 2