问题描述
我已经成功地使用subprocess.check_output来调用许多Windows程序.但是,我在打电话给icacl时遇到了麻烦.
通过cmd,这有效:cmd>icacls "C:\my folder" /GRANT *S-1-1-0:F
我试过了:subprocess.check_output(['C:\\Windows\\System32\\icacls.exe','"C:\\my folder"','/GRANT *S-1-1-0:F'],shell=True,stderr=subprocess.STDOUT)
但返回码是123(根据micrsoft,无效的文件名).
我也尝试过(也可以在cmd中使用)subprocess.check_output(['C:\\Windows\\System32\\icacls.exe','"C:/my folder"','/GRANT *S-1-1-0:F'],shell=True,stderr=subprocess.STDOUT)
但返回代码也是123.
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.
有什么主意吗?
推荐答案
不要过度引用您的参数,否则它们将按字面传递.在需要时,让check_output
处理报价.使用参数列表的最佳方法:
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)
(请注意,我删除了shell=True
和命令的路径,并使用了原始前缀来避免对文件夹参数使用反斜杠加倍)
(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)
这篇关于从python调用Windows的icacls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!