的Python: “ 'STR' 不支持缓冲区接口” 上subprocess.communicate(迁移到3.x)

的Python: “ 'STR' 不支持缓冲区接口” 上subprocess.communicate(迁移到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) 

该代码可以使用Python2.x但Python3.x它给了我:的Python: “ 'STR' 不支持缓冲区接口” 上subprocess.communicate(迁移到3.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 
+0

尝试'通讯(cmd_a.encode( “ASCII”))' –

在Python 3 subprocess流是二进制的。

要编写一个字符串,只是编码的二进制,你的情况ascii编解码器是OK:

fdisk_cmd.communicate(cmd_a.encode("ascii")) 
+0

谢谢,它的工作,不要”现在看到这个错误。但它会影响功能吗? – Milan

+0

你是什么意思?数据被传递给处理输入,你可以自己检查一下 –