Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python syntax in Linux
#3
(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 the run() function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.
Reply


Messages In This Thread
Python syntax in Linux - by St0rmcr0w - Jul-29-2021, 12:16 PM
RE: Python syntax in Linux - by DeaD_EyE - Jul-29-2021, 01:01 PM
RE: Python syntax in Linux - by snippsat - Jul-29-2021, 01:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Linux, Python, C ++, Brain2 and errors. PiotrBujakowski 0 962 Jun-24-2024, 03:41 PM
Last Post: PiotrBujakowski
  Is possible to run the python command to call python script on linux? cuten222 6 2,303 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Syntax error while executing the Python code in Linux DivAsh 8 4,763 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  where to get portable Python for Linux (Fedora)? python001 5 8,730 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  Python in Linux environment on RPI kendias 22 14,285 Sep-05-2020, 03:04 AM
Last Post: K_Research
  How do I pick the right python in Linux env? MDRI 9 5,427 Jun-27-2020, 05:40 PM
Last Post: snippsat
  control a linux program with python Fifoux082 9 5,530 May-08-2020, 04:24 PM
Last Post: Fifoux082
  Python version on Linux whois1230 5 4,713 Apr-10-2020, 07:12 PM
Last Post: buran
  Unable to install python-wnck python-imaging on Void Linux linuxlight 1 3,968 Mar-08-2020, 03:53 AM
Last Post: Larz60+
  Python in Kali Linux coolcassie 2 9,464 Dec-02-2019, 12:48 PM
Last Post: coolcassie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020