gzip a file in Python

There is a module gzip. Usage: Example of how to create a compressed GZIP file: import gzip content = b”Lots of content here” f = gzip.open(‘/home/joe/file.txt.gz’, ‘wb’) f.write(content) f.close() Example of how to GZIP compress an existing file: import gzip f_in = open(‘/home/joe/file.txt’) f_out = gzip.open(‘/home/joe/file.txt.gz’, ‘wb’) f_out.writelines(f_in) f_out.close() f_in.close() EDIT: Jace Browning’s answer using … Read more

Python spawn off a child subprocess, detach, and exit

For Python 3.8.x, the process is a bit different. Use the start_new_session parameter available since Python 3.2: import shlex import subprocess cmd = “<full filepath plus arguments of child process>” cmds = shlex.split(cmd) p = subprocess.Popen(cmds, start_new_session=True) This will allow the parent process to exit while the child process continues to run. Not sure about … Read more

Interactive input/output using Python

None of the current answers worked for me. At the end, I’ve got this working: import subprocess def start(executable_file): return subprocess.Popen( executable_file, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def read(process): return process.stdout.readline().decode(“utf-8″).strip() def write(process, message): process.stdin.write(f”{message.strip()}\n”.encode(“utf-8”)) process.stdin.flush() def terminate(process): process.stdin.close() process.terminate() process.wait(timeout=0.2) process = start(“./dummy.py”) write(process, “hello dummy”) print(read(process)) terminate(process) Tested with this dummy.py script: #!/usr/bin/env python3.6 import … Read more

How to write to stdout AND to log file simultaneously with Popen?

You can use a pipe to read the data from the program’s stdout and write it to all the places you want: import sys import subprocess logfile = open(‘logfile’, ‘w’) proc=subprocess.Popen([‘cat’, ‘file’], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in proc.stdout: sys.stdout.write(line) logfile.write(line) proc.wait() UPDATE In python 3, the universal_newlines parameter controls how pipes are used. If False, … Read more

Using sudo with Python script

Many answers focus on how to make your solution work, while very few suggest that your solution is a very bad approach. If you really want to “practice to learn”, why not practice using good solutions? Hardcoding your password is learning the wrong approach! If what you really want is a password-less mount for that … Read more

Passing double quote shell commands in python to subprocess.Popen()?

I’d suggest using the list form of invocation rather than the quoted string version: command = [“ffmpeg”, “-i”, “concat:1.ts|2.ts”, “-vcodec”, “copy”, “-acodec”, “copy”, “temp.mp4”] output,error = subprocess.Popen( command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() This more accurately represents the exact set of parameters that are going to be passed to the end process and eliminates the need to … Read more