Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python syntax in Linux
#1
I am currently learning Python in school and have truly enjoyed it. This week we are to working with subprocess. we are to run commands and take screenshots, but the commands they have given us are all for Windows and I run Linux. out of the 15 commands I have figured out all except for one.

The question says:
Use subprocess.Popen to execute the Windows dir command and have its output placed in a text file named pythonOut.txt Hint: The argument for Popen in this case will be subprocess.Popen('C:\\windows\\system32\\cmd.exe "/c dir C:\\ >> C:\\pythonOut.txt"')

I have been racking my brain to find how to do this. 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?

Thank you for your help,

St0rmcr0w
Reply
#2
You need to use subprocess.PIPE for stdout or use a file-like object.
If you open a file, you've a file like object. Popen expects the write method on the stdout/stderr object.


from pathlib import Path
import subprocess

def run_stdout_save(cmd, file_append):
    """
    Append stdout of cmd to a file.
    This is a blocking function
    """
    # open the file in append raw bytes mode
    # this will prevent encoding errors
    with open(file_append, "ab") as fd:

        # suppress window
        startup_info = subprocess.STARTUPINFO()
        startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        proc = subprocess.Popen(cmd, stdout=fd, startupinfo=startup_info)
        # wait until it's finished
        proc.wait()


# using pathlib to create the paths
stdout_file = Path.home() / "Desktop/proc_stdout.txt"

cmd = "C:\\windows\\system32\\cmd.exe"
# each argument is a str element in the list.
run_stdout_save([cmd, "/c",  "dir", "C:\\"], stdout_file)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 710 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Syntax error while executing the Python code in Linux DivAsh 8 1,526 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  How to use a variable in linux command in python code? ilknurg 2 1,584 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Login to NordVPN on Linux with python script AGreenPig 2 5,947 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  how to run linux command with multi pipes by python !! evilcode1 2 6,300 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  where to get portable Python for Linux (Fedora)? python001 5 6,274 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  Python in Linux environment on RPI kendias 22 11,039 Sep-05-2020, 03:04 AM
Last Post: K_Research
  How do I pick the right python in Linux env? MDRI 9 3,634 Jun-27-2020, 05:40 PM
Last Post: snippsat
  control a linux program with python Fifoux082 9 4,073 May-08-2020, 04:24 PM
Last Post: Fifoux082
  Python version on Linux whois1230 5 3,460 Apr-10-2020, 07:12 PM
Last Post: buran

Forum Jump:

User Panel Messages

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