Python代码通过命令行发送命令

final="cacls " + "E:/" + "\"" + list1[2] + " " + list1[3] + "\"" + " /p " + str os.system(final) 

我试图设置权限到一个文件夹使用Python,但在运行此命令时,用户input也需要提供,即

它会询问你是否(Y / N),用户需要input“Y”或“N”

有没有办法使用python发送用户input“Y”以及上面的代码?

  pro = subprocess.Popen(final,shell=True, stdin=subprocess.PIPE) pro.communicate(bytes("Y\r\n",'utf-8')) 

我已经添加了下面的代码。 该程序退出而不设置权限。

http://jimmyg.org/blog/2009/working-with-python-subprocess.html#writing-to-standard-input

Solutions Collecting From Web of "Python代码通过命令行发送命令"

尝试使用子流程模块

 import subprocess cmd = ["cacls", "E:/" + list1[2], list1[3], "/p", str] pro = subprocess.Popen(final, stdin=subprocess.PIPE) pro.communicate("y\r\n") 

作为一个聪明的程序员,使用PBS

那么,代码是:

 from pbs import type as echo# Isn't it echo for Windows? If not, use the correct one script = Command("/path/to/cacls ") print script(echo("Y"), ("E:/" + "\"" + list1[2] + " " + list1[3] + "\"" + " /p " + str).split())