pythonwindowscmdicacls

calling windows' icacls from python


I've used successfully subprocess.check_output to call plenty of windows programs. Yet, I'm troubled at calling icacls.
By cmd, this works:
cmd>icacls "C:\my folder" /GRANT *S-1-1-0:F
I've tried:
subprocess.check_output(['C:\\Windows\\System32\\icacls.exe','"C:\\my folder"','/GRANT *S-1-1-0:F'],shell=True,stderr=subprocess.STDOUT)
but return code is 123 (according to micrsoft, invalid file name).
I've also tried (which also works from cmd)
subprocess.check_output(['C:\\Windows\\System32\\icacls.exe','"C:/my folder"','/GRANT *S-1-1-0:F'],shell=True,stderr=subprocess.STDOUT)
but return code is also 123.

Any idea?


Solution

  • don't overquote your arguments, or they are passed literally. Let check_output handle the quoting when needed. Best way using a list of arguments:

    subprocess.check_output(['icacls.exe',r'C:\my folder','/GRANT','*S-1-1-0:F'],stderr=subprocess.STDOUT)
    

    (note that I removed shell=True and the path to the command, and used raw prefix to avoid doubling the backslashes for the folder argument)