Click here to Skip to main content
15,943,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have used exec in python for execution some command in python

What I have tried:

resp=  'a = 4; print(\"Expected a = 4. Actual a = {} \".format(a))'

now i exec this command now i want to capture through the terminal

what i tried is

proc = subprocess.Popen(['-ls','-l',],Path.home(),text=True ,stdout=subprocess.PIPE)
                    outs, errs = proc.communicate(timeout=15)
                except TimeoutExpired:
                outs, errs = proc.communicate()

what i am geting error like below

p = subprocess.Popen('ls','-l', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "C:\test\lib\", line 753, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

please Can you help me?
I am using python3.8
Updated 16-Aug-22 3:05am
Richard MacCutchan 16-Aug-22 9:30am    
You are passing the two strings as separate arguments to Popen. But Popen requires either a single string or a list:
p = subprocess.Popen('ls -l', ...
# or
p = subprocess.Popen(['ls','-l'], 
Richard MacCutchan 16-Aug-22 9:51am    
What is Path.home() supposed to mean? As I already suggested, stop guessing and read the documentation.
Richard MacCutchan 17-Aug-22 7:53am    
There is no ls command in Windows. It is a Unix/Linux shell command. Whatever is is you are trying to achieve with subprocess, exec etc, you really need a better understanding of how the Windows system (and Python) works.
Richard MacCutchan 17-Aug-22 8:50am    
Like I said you need to get a better understanding of Windows. You cannot start a process with an internal command in Windows, you must execute "cmd.exe" and pass it the command string as its argument.
    s = subprocess.check_output(["cmd", "/c", "dir"])
Richard MacCutchan 17-Aug-22 11:18am    
And here is a version with your Python code:
    pypath = 'C:/Users/< path to your python executable >/python'
    pyopt = '-c'
    pycmd = 'a = 4; print("Expected a = 4. Actual a = {} ".format(a))'
    output =[pypath, pyopt, pycmd], capture_output=True)
    print(F'{output.stdout = }')

Share this answer
HelpMewithCode 16-Aug-22 9:06am    
This is not what I except OriginalGriff

I have updated my code
please Can you look into this
When you get an error telling you that a parameter is missing or invalid, then you should go immediately to the documentation to find out why: subprocess — Subprocess management — Python 3.10.6 documentation[^].

You also seem to be trying to run two different things. In the Python code you are trying to execute a Linux/UNIX ls shell command. In the (I assume C#) code you are trying to exec a string of Python text.

And it is not clear which of these you want to capture.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900