Search code examples
pythonechotee

write to file with tee command in python


I have a python script with variable names e.g V and It. I make a filename based on the parameter as following:

 file_out=io.open("file_Iter" + str(It) + "_V_" +str(V)+".txt", 'w')

Then I'd like to redirect all my terminal output to a this file, so I use this command:

 os.system("echo - START RUN $(LANG=en_US date +%b_%d_%Y_%k_%M)- | tee -a $file_out")

The file_out is created and also the echo command is shown correctly on the terminal but it is not written to file_out. If in tee command, I put e.g tee testfile.txt, then this file is created and also echo command writes in it.

Q: How should I change tee in order to write to the file that is created by variable names?


Solution

  • I'm not sure I understood it correctly, but I guess that what you want to do is the following:

    fileName = "file_Iter" + str(It) + "_V_" +str(V)+".txt"
    file_out=io.open(fileName, 'w')
    os.system("echo - START RUN $(LANG=en_US date +%b_%d_%Y_%k_%M)- | tee -a " + fileName)
    

    Pay attention to the end of the command, where fileName is concatenated.