【发布时间】:2017-01-09 20:21:20
【问题描述】:
我想在关闭子进程后继续执行命令。我有以下代码,但 fsutil 未执行。我该怎么做?
import os
from subprocess import Popen, PIPE, STDOUT
os.system('mkdir c:\\temp\\vhd')
p = Popen( ["diskpart"], stdin=PIPE, stdout=PIPE )
p.stdin.write("create vdisk file=c:\\temp\\vhd\\test.vhd maximum=2000 type=expandable\n")
p.stdin.write("attach vdisk\n")
p.stdin.write("create partition primary size=10\n")
p.stdin.write("format fs=ntfs quick\n")
p.stdin.write("assign letter=r\n")
p.stdin.write("exit\n")
p.stdout.close
os.system('fsutil file createnew r:\dummy.txt 6553600') #this doesn´t get executed
【问题讨论】:
-
请不要再使用
os.system()。它非常老式,建议弃用很长时间。 -
如果不在我的代码中包含 fsutil 行,您如何建议?
-
不应该是
p.stdout.close()吗?我认为您缺少一些括号。 -
其实你不是在
p.stdin.write("exit\n")后面少了一个p.communicate()吗? -
加上 () 得到了相同的结果。 p.communicate()?那会怎么样?
标签: python subprocess stdout