Try separating the values with commas:
subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username])
See http://docs.python.org/library/subprocess.html#subprocess.call - It takes an array where the first argument is the program and all other arguments are passed as arguments to the program.
Also don't forget to check the return value of the function for a zero return code which means "success" unless it doesn't matter for your script if the user was added successfully or not.
Answer from ThiefMaster on Stack OverflowTry separating the values with commas:
subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username])
See http://docs.python.org/library/subprocess.html#subprocess.call - It takes an array where the first argument is the program and all other arguments are passed as arguments to the program.
Also don't forget to check the return value of the function for a zero return code which means "success" unless it doesn't matter for your script if the user was added successfully or not.
Try to add commas between your list items:
subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, \
'-u' ,_userid, _username])
subprocess.call takes the same arguments as subprocess.Popen:
argsshould be a string, or a sequence of program arguments.
Edit
To turn all your arguments into strings at once you could you a list comprehension:
args = ['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, \
'-u' ,_userid, _username]
str_args = [ str(x) for x in args ]
Hello,
I'm trying to launch a powershell script via subprocess.call or subprocess.run, but I'm having trouble with how to specify the arguments for a function in powershell.
For example, in a powershell script I have a function Foo which accepts a string. With subprocess I would like to launch this script and pass an argument to Foo.
How can I do that?
In order to simply call the script I managed to do it in the following way:
if is_admin():
# If launched as admin - good
cmd = ["PowerShell", "-ExecutionPolicy", "Unrestricted", "-File", absolute_path]
ec = subprocess.call(cmd)
print("Powershell returned: {0:d}".format(ec))
else:
# If not admin, relaunch as admin
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)How to pass arguments in python subprocess? - Stack Overflow
python - How to pass arguments to subprocess - Stack Overflow
How do I pass arguments to another function with subprocess.Popen?
Python subprocess arguments - Stack Overflow
As explained here: https://docs.python.org/3/library/subprocess.html, you can add in the array every argument you would normally pass in a command-line.
For example, if you wanted to run ls -l --color=auto, you could write:
subprocess.run(["ls", "-l", "--color=auto"])
Here, that would be:
subprocess.run(["python3", "detect_wrong.py", "--source", "overpass.mp4", "--weights", "./my_coco.pt", "--data", "./data/my_coco.yaml"])
However, since you want to run a Python script from Python, I suggest you take a look here: How can I make one python file run another? and, as said there, treat detect_wrong.py as a module, if possible.
Else, you can run it with exec() or, if desperate, with os.system() or subprocess.run()
you can simply pass arguments as entities on the list, like that:
subprocess.run(["python", "--argument=value", "detect_wrong.py"]
Hi! I use Subprocess.Popen to specificly call a function from another python module/script.
This works:
subprocess.Popen(["python", "-c","import " + "main" + ";" + "main" + "." + "func" + "()"])
However, say I have some global variables from the python script I call this function, how would I pass these into subprocess.Popen? Like this:
arg1 = 2, arg2 = 4
subprocess.Popen(["python", "-c","import " + "main" + ";" + "main" + "." + "func" + "(arg1, arg2)"])
Above example does not work. How can I make it work?
As JBernardo mentioned in a comment, separate the "-bufsize 4096" argument into two, "-bufsize", "4096". Each argument needs to be separated when subprocess.call is used with shell=False (the default). You can also specify shell=True and give the whole command as a single string, but this is not recommended due to potential security vulnerabilities.
You should not need to use string formatting where you have "%s" % url. If url is a string, pass it directly, otherwise call str(url) to get a string representation.
This is the way to go:
url = 'http://www.whatever.com'
cmd = 'ffplay -vn -nodisp -bufsize 4096 '.split()
subprocess.call(cmd + [str(url)], shell=False)