问题描述
我已经成功地使用 subprocess.check_output 来调用大量的 Windows 程序.然而,我在调用 icacls 时遇到了麻烦.
通过 cmd,这是有效的:cmd>icacls "C:\my folder"/GRANT *S-1-1-0:F
我试过了:subprocess.check_output(['C:\\Windows\\System32\\icacls.exe','"C:\\我的文件夹"','/GRANT *S-1-1-0:F'],shell=True,stderr=subprocess.STDOUT)
但返回码是 123(根据 micrsoft,无效文件名).
我也试过(也适用于 cmd)subprocess.check_output(['C:\\Windows\\System32\\icacls.exe','"C:/我的文件夹"','/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)
这篇关于调用 windows'来自 python 的 icacls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!