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上它给出了:
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
最佳答案
在python 3中,subprocess
流是二进制的。
要编写字符串,只需编码二进制文件,在您的情况下ascii
codec是可以的:
fdisk_cmd.communicate(cmd_a.encode("ascii"))
关于python - Python:subprocess.communicate上的“'str'不支持缓冲区接口(interface)”(迁移到3.x),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46191997/