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

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

问题描述:

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

我想设置权限使用Python中的文件夹,但在运行此命令时,用户输入需要提供过于即Python代码通过命令行发送命令

它会询问ARE YOU SURE(Y/N)和用户需要输入“Y”或“N”

是否有任何方式使用python发送用户输入“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

+0

Aparté酒店:这不是PHP或Javascript - Python可以读取连接的可读方式宁弦,他们好多了。例如,尝试:'final =“cacls%s \”%s%s \“/ p%s”%(“E:/”,list1 [2],list1 [3],str) – jsbueno 2012-04-05 13:03:19

+0

谢谢,我会这样做:) – 2012-04-06 03:56:45

尝试使用子模块

import subprocess 
cmd = ["cacls", "E:/" + list1[2], list1[3], "/p", str] 

pro = subprocess.Popen(final, stdin=subprocess.PIPE) 
pro.communicate("y\r\n") 
+0

嗨jakob,我试过你的解决方案,该程序永远不会退出,权限未设置 – 2012-04-05 12:20:31

+1

尝试编辑版本。 – 2012-04-05 12:22:05

+0

相同的输出,这次我可以看到正在执行的cacls.exe。该程序不退出。 – 2012-04-05 12:24:08

作为一个聪明的程序员,使用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())