Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 15 characters in body
Source Link

I have a collection of Bash scripts which I want to recreate in python. One of the key feature of these scripts is when i execute them it will save the contents of the terminal into a logfile. In Bash simply i used the tee command.

2>&1 | tee "logfile.txt"; 

the problem is to find equal solution for python.

I found two half of this "puzzle" so far (solution A and B), one of the expected behaviour works in one of the scripts but not in the other and vice versa.

solution A)

#!/usr/bin/env python3   import sys from subprocess import Popen, PIPE, STDOUT   with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, stderr=STDOUT, bufsize=1) as p, \   open('logfile.txt', 'ab') as file: for line in p.stdout: sys.stdout.buffer.write(line) file.write(line) 

solution B)

#!/usr/bin/env python3 import sys from subprocess import Popen, PIPE   import sys logfile = open('logfile.txt', 'w') with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, bufsize=1, universal_newlines=True) as p: logfile = open('logfile.txt', 'w') for line in p.stdout: print(line, end='') 

i tried to "merge" the features of these 2 code snippets, but i cant figure it out, how to put it together.

What i looking for is the EXACT behavior replication of the tee command in a python script file. which means...

  • the contents of the terminal appear in the terminal window AND saved into a log file (just like solution A)

  • when i start the python script file, i want to follow the progress of the process in the terminal, to check how far is from completion (just like solution B). I dont want to stare at a blank screen until the process completes (solution A).

I would appreciate the help.

for testing i use a webm format file (downloaded with youtube-dl) and convert it to mp3 with ffmpeg in cygwin. you can download the ffmpeg binary from here if you want experimenting with it https://www.ffmpeg.org/download.html

Thank you!

I have a collection of Bash scripts which I want to recreate in python. One of the key feature of these scripts is when i execute them it will save the contents of the terminal into a logfile. In Bash simply i used the tee command.

2>&1 | tee "logfile.txt"; 

the problem is to find equal solution for python.

I found two half of this "puzzle" so far (solution A and B), one of the expected behaviour works in one of the scripts but not in the other and vice versa.

solution A)

#!/usr/bin/env python3 import sys from subprocess import Popen, PIPE, STDOUT with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, stderr=STDOUT, bufsize=1) as p, \   open('logfile.txt', 'ab') as file: for line in p.stdout: sys.stdout.buffer.write(line) file.write(line) 

solution B)

#!/usr/bin/env python3 from subprocess import Popen, PIPE   import sys logfile = open('logfile.txt', 'w') with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: print(line, end='') 

i tried to "merge" the features of these 2 code snippets, but i cant figure it out, how to put it together.

What i looking for is the EXACT behavior replication of the tee command in a python script file. which means...

  • the contents of the terminal appear in the terminal window AND saved into a log file (just like solution A)

  • when i start the python script file, i want to follow the progress of the process in the terminal, to check how far is from completion (just like solution B). I dont want to stare at a blank screen until the process completes (solution A).

I would appreciate the help.

for testing i use a webm format file (downloaded with youtube-dl) and convert it to mp3 with ffmpeg in cygwin. you can download the ffmpeg binary from here if you want experimenting with it https://www.ffmpeg.org/download.html

Thank you!

I have a collection of Bash scripts which I want to recreate in python. One of the key feature of these scripts is when i execute them it will save the contents of the terminal into a logfile. In Bash simply i used the tee command.

2>&1 | tee "logfile.txt"; 

the problem is to find equal solution for python.

I found two half of this "puzzle" so far (solution A and B), one of the expected behaviour works in one of the scripts but not in the other and vice versa.

solution A)

#!/usr/bin/env python3   import sys from subprocess import Popen, PIPE, STDOUT   with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, stderr=STDOUT, bufsize=1) as p, \ open('logfile.txt', 'ab') as file: for line in p.stdout: sys.stdout.buffer.write(line) file.write(line) 

solution B)

#!/usr/bin/env python3 import sys from subprocess import Popen, PIPE with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, bufsize=1, universal_newlines=True) as p: logfile = open('logfile.txt', 'w') for line in p.stdout: print(line, end='') 

i tried to "merge" the features of these 2 code snippets, but i cant figure it out, how to put it together.

What i looking for is the EXACT behavior replication of the tee command in a python script file. which means...

  • the contents of the terminal appear in the terminal window AND saved into a log file (just like solution A)

  • when i start the python script file, i want to follow the progress of the process in the terminal, to check how far is from completion (just like solution B). I dont want to stare at a blank screen until the process completes (solution A).

I would appreciate the help.

for testing i use a webm format file (downloaded with youtube-dl) and convert it to mp3 with ffmpeg in cygwin. you can download the ffmpeg binary from here if you want experimenting with it https://www.ffmpeg.org/download.html

Thank you!

Source Link

Replication of the Bash tee command behavior in python

I have a collection of Bash scripts which I want to recreate in python. One of the key feature of these scripts is when i execute them it will save the contents of the terminal into a logfile. In Bash simply i used the tee command.

2>&1 | tee "logfile.txt"; 

the problem is to find equal solution for python.

I found two half of this "puzzle" so far (solution A and B), one of the expected behaviour works in one of the scripts but not in the other and vice versa.

solution A)

#!/usr/bin/env python3 import sys from subprocess import Popen, PIPE, STDOUT with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, stderr=STDOUT, bufsize=1) as p, \ open('logfile.txt', 'ab') as file: for line in p.stdout: sys.stdout.buffer.write(line) file.write(line) 

solution B)

#!/usr/bin/env python3 from subprocess import Popen, PIPE import sys logfile = open('logfile.txt', 'w') with Popen(['ffmpeg','-i','1.webm','-y','1.mp3'], stdout=PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: print(line, end='') 

i tried to "merge" the features of these 2 code snippets, but i cant figure it out, how to put it together.

What i looking for is the EXACT behavior replication of the tee command in a python script file. which means...

  • the contents of the terminal appear in the terminal window AND saved into a log file (just like solution A)

  • when i start the python script file, i want to follow the progress of the process in the terminal, to check how far is from completion (just like solution B). I dont want to stare at a blank screen until the process completes (solution A).

I would appreciate the help.

for testing i use a webm format file (downloaded with youtube-dl) and convert it to mp3 with ffmpeg in cygwin. you can download the ffmpeg binary from here if you want experimenting with it https://www.ffmpeg.org/download.html

Thank you!