Mil*_*lan 2 python subprocess python-2.7 python-3.x
cmd_a = "a\n{0}\nt\n{0}\nda\nw\n".format(number)
cmd_b = subprocess.Popen("fdisk %s" % file_name, shell=True,stdin=subprocess.PIPE)
fdisk_cmd.communicate(cmd_a)
Run Code Online (Sandbox Code Playgroud)
这段代码适用于 Python2.x,但在 Python3.x 上它给了我:
File "bootimage.py", line 44, in do_install_file
| fdisk_cmd.communicate(cmd_a)
| File "/usr/lib/python3.4/subprocess.py", line 930, in communicate
| self.stdin.write(input)
| TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud)
在 python 3 中,subprocess流是二进制的。
要编写字符串,只需编码二进制文件,在您的情况下ascii编解码器就可以:
fdisk_cmd.communicate(cmd_a.encode("ascii"))
Run Code Online (Sandbox Code Playgroud)