Jul-29-2021, 01:40 PM
(Jul-29-2021, 12:16 PM)St0rmcr0w Wrote: I have found that subprocess.Popen(['ls', '-a']) will pull a directory and display it on the screen but how do I output that into a file using a single command line as the Windows example above?I would do it like this on Linux.
So run() that should be used in most cases got new
capture_output
parameter in Python 3.7>.import subprocess ls_files = subprocess.run(["ls", "-l", "/home/tom"], capture_output=True) print(ls_files.stdout.decode()) with open('ls_out.txt', 'w') as f: f.write(ls_files.stdout.decode())
subprocess dok Wrote:The recommended approach to invoking subprocesses is to use therun()
function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.